2013-02-18 44 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))