如何在不遇到麻烦的情况下使用PHP执行查询?

时间:2012-01-21 11:19:21

标签: php sql date time range

嘿伙计们,我有两个日期输入,每个日期,月份和年份包含三个保险箱。 day有一个名字d1,month = m2,year = y1,同样的第二个日期输入它的d2,m2和y2

我正在尝试查询数据库以查找特定日期范围之间的信息。

  $date1=date("Y-m-d", mktime(0, 0, 0, $_POST['m1'], $_POST['d1'],$_POST['y1']));
  $date2=date("Y-m-d", mktime(0, 0, 0, $_POST['m2'], $_POST['d2'],$_POST['y2']));

  echo $date1." and ".$date2;

$query="SELECT *FROM FDB where time BETWEEN ".$date1." and ".$date2;
$f = mysql_query($query) or die(mysql_error());
$nfiles= mysql_num_rows($f);

$query="SELECT *FROM usr where created BETWEEN ".$date1." and ".$date2;
$f = mysql_query($query) or die(mysql_error());
$nus= mysql_num_rows($f);

下面的行显示错误
$query="SELECT *FROM history WHERE ACTIVITY='Logged In' where CAST(FLOOR(CAST(time AS FLOAT))AS DATETIME) between ".$date1." AND ".$date2;

错误是

          You have an error in your SQL syntax; check the manual that corresponds to  your MySQL server version for the right syntax to use near 'where CAST(FLOOR(CAST(time AS FLOAT))AS DATETIME) between `2011-01-01` AND `2012' at line 1

我如何更改代码以便不再出现错误?

2 个答案:

答案 0 :(得分:2)

您的查询中有两个WHERE个。将第二个替换为AND

大多数“你的查询中有错误”消息指向MySQL混淆的确切字符,所以你应该在之前找到错误

答案 1 :(得分:2)

您的代码中有2个WHERE子句。

试试这种方式

$query="SELECT * FROM history 
        WHERE ACTIVITY='Logged In' 
            AND CAST(FLOOR(CAST(time AS FLOAT))AS DATETIME) 
            between ".$date1." AND ".$date2;