2013-02-18 43 views
0

我有2個變量持有日期。當我使用變量時,我在SQL語句中出現語法錯誤。變量 - PHP MYSQL

$from_date = '2013-02-13'; 
$to_date = '2013-02-20'; 
$query="SELECT * FROM mytable where datex >= ".$fromdate." AND datex <= ".$todate."; 

幫助我找出並糾正這個語法錯誤?

謝謝。

回答

6

你需要引用您的變量:

$query="SELECT * FROM mytable where datex >= '".$fromdate."' AND datex <= '".$todate." . "'"; 

但它會更好地使用準備好的語句綁定變量。然後您的查詢可能看起來像(PDO):

$query="SELECT * FROM mytable where datex >= :fromdate AND datex <= :todate"; 
+1

+1我說同樣的事情,刪除了我的答案。 :) – diggersworld 2013-02-18 17:24:29

0

即時做類似的事情會這樣做是正確的方法嗎?

<?php 
    include 'connect.php'; 

    $id1 = $_POST['PatientID']; //Text box the user searches in 
    $result = mysqli_query($con,"SELECT * FROM PatientRecords WHERE PatientID= '".$id1."'"); 
    while($row = mysqli_fetch_array($result))