使用带有日期的Between子句和和子句作为mysql中的条件

时间:2017-07-18 11:01:42

标签: php mysql pdo

我想从消耗表中检索数据,并在查询中应用条件。当我在PHP中回显查询并在MySQL中运行它时返回此错误:#1054 -

  

'where子句'中的未知栏'2017-07-14'


P.S:timestamp列数据类型是datetime。

$drink = $_POST['drink'];
$user = $_SESSION['name'];
$from = $_POST['from'];
$to = $_POST['to'];
$selectquery = " SELECT * FROM consumptions 
                 WHERE (time_stamp >=`$from`  AND time_stamp<=`$to`) 
                AND user_name = `$user` AND `d_id` = `$drink`";

任何帮助都将受到高度赞赏。

2 个答案:

答案 0 :(得分:0)

这是因为您使用了`而不是&#39;

`用于不是值

的列的名称

更改为

    `SELECT * FROM `consumptions` 
     WHERE (`time_stamp` >= '$from' AND `time_stamp` <= '$to') 
     AND `user_name` = '$user' AND `d_id` = '$drink'";)`

也可以在MYSQL中使用

WHERE `time_stamp` BETWEEN '$from' AND '$to'

并且你的代码也可以通过sql注入。

答案 1 :(得分:0)

SQL:

$selectquery = "SELECT * FROM consumptions 
                WHERE (time_stamp BETWEEN `$from`  AND `$to`) 
                AND user_name = `$user` AND `d_id` = `$drink`";