神秘的MYSQL语法错误 - 在phpMyadmin中工作正常,而不是在页面上

时间:2015-04-13 03:54:01

标签: php mysql

尝试从特定日期获取费用列表,我可以在PHPMyAdmin中执行此SQL,但在页面上我收到MYSQL语法错误。

守则:

$full_date = date('d M Y', strtotime($date));

$get_expenses = mysql_query("SELECT Sum(cost) AS expense FROM expenses WHERE date = $full_date")or die(mysql_error());

$expenses = mysql_fetch_array( $get_expenses );

echo $expenses['expense'];

我收到的语法错误是:

  

您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,以便在第4行“2015年4月”附近使用正确的语法

我已查看$expenses['expense']的值,并返回有效日期。在PHPMyAdmin中,我可以毫无问题地执行此SQL。有什么指针吗?干杯!

2 个答案:

答案 0 :(得分:0)

尝试这样,在将日期转换为相应格式之前,请执行此操作,

$get_expenses = mysql_query("SELECT Sum(cost) AS expense FROM expenses WHERE date = '$full_date'")or die(mysql_error());

答案 1 :(得分:0)

SELECT Sum(cost) AS expense FROM expenses WHERE date = 13 Apr 2015

是一个有效的查询,但它是你从该表达式获得的。

您应该使用引号括起值,例如:

$full_date = date('Y-m-d', strtotime($date));
$get_expenses = mysql_query("SELECT Sum(cost) AS expense FROM expenses WHERE date = '$full_date'") or die(mysql_error());