开发者

PHP and MYSQL: Why does A work and B not work?

开发者 https://www.devze.com 2022-12-11 04:57 出处:网络
First of all, I am using the DATE_ADD function for MySQL. When trying to use $sqlA in php, it saids syntax error for some reason (mainly the area after WHE开发者_运维知识库RE). Why?

First of all, I am using the DATE_ADD function for MySQL. When trying to use $sqlA in php, it saids syntax error for some reason (mainly the area after WHE开发者_运维知识库RE). Why?

$sqlA = "SELECT $column_name FROM $table_name WHERE Date >= DATE_ADD(CURDATE(), - INTERVAL 3 DAY)";

However, it works without DATE_ADD:

$sqlB = "SELECT column FROM table WHERE Date >=  CURDATE() - INTERVAL 3 DAY ";

Notice I put the column name and table name in $sqlB? Well, I tried substituting it now with PHP and this fails. It says syntax error, why?

$sqlC = "SELECT $column_name FROM $table_name WHERE Date >=  CURDATE()-INTERVAL 3 DAY";


You can't have the minus sign in front of INTERVAL. Use DATE_SUB instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消