2013-10-07 93 views
0

我在MySQL中有一個查詢工作正常,但是當我將查詢複製到我的PHP文件中時,它引發了一個語法錯誤:** unexpected「)爲week_ending **什麼是我失蹤PHP腳本中的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())';

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

回答

4

有你的SQL被提前終止您的要求的中間一對夫婦的單引號你只需要逃避。現狀這裏附近TES:

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

像這樣:

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

除了沒有逃脫包含引號,你還沒有關閉的方法調用。

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