简体   繁体   English

PHP脚本中的MySQL语法错误

[英]MySQL Syntax Error in PHP Script

I have a query in MySQL that works fine, however when i copy the query over into my PHP file, it is throwing me a syntax error: ** unexpected ") as week_ending ** what am i missing? 我在MySQL中有一个可以正常工作的查询,但是当我将查询复制到我的PHP文件中时,它向我抛出了语法错误:**意外的“)as week_ending **我缺少什么?

MySQL: MySQL:

'SELECT COUNT(*) as count , region , DATE_FORMAT(NOW(),'%d %b %y') as week_ending FROM stores.stats WHERE date > DATE_ADD(DATE(NOW()), INTERVAL -1 WEEK) AND date < DATE(NOW()) GROUP BY region , DATE(NOW())'; 将SELECT COUNT(*)作为countregion ,DATE_FORMAT(NOW(),'%​​d%b%y')作为week_ending FROM stores.stats date > DATE_ADD(DATE(NOW()),间隔-1周)和date <DATE(NOW())GROUP BY region ,DATE(NOW())';

PHP: PHP:

$stmt = DB::query(Database::SELECT, 'SELECT COUNT(*) as count , region , DATE_FORMAT(NOW(),'%d %b %y'), as week_ending FROM stores.stats WHERE date > DATE_ADD(DATE(NOW()), INTERVAL -1 WEEK) AND date < DATE(NOW()) GROUP BY region , DATE(NOW())'; $ stmt = DB :: query(Database :: SELECT,'SELECT COUNT(*)as countregion ,DATE_FORMAT(NOW(),'%​​d%b%y'),以week_ending结尾。统计date WHERE date > DATE_ADD (DATE(NOW()),间隔-1周)和date <DATE(NOW())GROUP BY region ,DATE(NOW())';

There are a couple single quotes in the middle of your SQL that are prematurely terminating your request. SQL中间有几个单引号,它们会提前终止您的请求。 You just need to escape the quotes near here: 您只需要在此处转义引号即可:

DATE_FORMAT(NOW(),'%d %b %y'),

Like this: 像这样:

DATE_FORMAT(NOW(),\'%d %b %y\'),

Besides not escaping contained quotes, you're also not closing the method call. 除了不转义包含的引号外,您还没有关闭方法调用。

$stmt = DB::query(
    Database::SELECT,
    'SELECT COUNT(*) as count,region, DATE_FORMAT(NOW(),\'%d %b %y\'), as week_ending FROM stores.stats WHERE date > DATE_ADD(DATE(NOW()), INTERVAL -1 WEEK) AND date < DATE(NOW()) GROUP BY region, DATE(NOW())'
);

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

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