简体   繁体   English

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

[英]Mysterious MYSQL syntax error - works fine in phpMyadmin, not on page

Trying to get a list of expenses from a certain date, I can execute this SQL fine in PHPMyAdmin but on the page I am receiving a MYSQL Syntax Error. 尝试获取特定日期的费用列表,我可以在PHPMyAdmin中执行此SQL罚款,但是在页面上我收到MYSQL语法错误。

The Code: 编码:

$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'];

The syntax error I receive is: 我收到的语法错误是:

You have an error in your SQL syntax; 您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Apr 2015' at line 1 检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在第1行的“ Apr 2015”附近使用

I have checked to see the value of $expenses['expense'] and it returns a valid date. 我检查了一下$expenses['expense'] ,它返回了一个有效的日期。 In PHPMyAdmin I can execute this SQL with no problems. 在PHPMyAdmin中,我可以毫无问题地执行此SQL。 Any pointers? 有指针吗? Cheers! 干杯!

尝试这样,在将您的日期转换成相应的格式之前,请像这样,

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

is not a valid query, but it's what you get from that expression. 不是有效的查询,但这是您从该表达式中获得的信息。

You should surround the value with quotes, such as with: 您应该用引号将值引起来,例如:

$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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM