2014-11-05 83 views
0

我有一個查詢問題:語法錯誤在MySQL LIMIT子句

$sth = $Db->dbh->prepare(
    "SELECT * 
    FROM loader 
    WHERE download = 0 
    AND lastconnected BETWEEN DATE_SUB(NOW(),INTERVAL 15 MINUTE) AND NOW() 
    ORDER BY lastconnected DESC 
    LIMIT :amount"); 

極限工作​​不因某種原因,如果我改變:量的硬編碼數,它會工作,但只要我用它作爲:量它給我這個錯誤:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''1'' at line 5 

這是我用來執行準備好的查詢的內容:

$sth->execute(array(':amount' => $amount)); 

試圖找出現在幾個小時。希望有人能看到我不是。

+1

這是把單引號的金額?這可能會導致問題。 – AdamMc331 2014-11-05 16:09:55

+0

明顯的問題:你在寫什麼語言?您是否嘗試過打印「:金額」以查看它是否具有有效值? – PaulJ 2014-11-05 16:10:18

+0

$ amount的值是多少?請在此處指定您的金額變量。 – 2014-11-05 16:11:00

回答

0

如果您將變量LIMIT與PDO一起使用,則必須將每個參數與PDOStatement::bindParam()綁定,並明確指定該參數的值具有整數類型(PDO::PARAM_INT)。使用輸入參數值數組的PDOStatement::execute()將所有值視爲字符串(PDO::PARAM_STR),而不是實際的PHP類型,如同沒有類型調用PDOStatement::bindParam()一樣,但MySQL的LIMIT關鍵字不接受字符串參數。這記錄在the manual page for the method PDOStatement::execute()中,並且有一個開放的feature request to change the behavior

+0

Tepples,所以我不能傳遞數組中的整數? – user2879055 2014-11-05 16:13:47

+0

@ user2879055將整數傳遞給'bindParam()'而不是數組。 – 2014-11-05 16:15:25

+0

我把它做成 \t \t \t \t ' $某物= $ DB-> dbh->準備(「SELECT * FROM裝載機WHERE下載= 0和\t \t \t \t \t \t \t \t lastconnected之間DATE_SUB(NOW( ),INTERVAL 15 MINUTE)\t \t \t \t \t \t \t \t \t \t AND NOW()\t \t \t \t \t \t \t \t \t ORDER BY lastconnected DESC \t \t \t \t \t \t \t \t \t LIMIT:量「); $ sth-> bindParam(':amount',$ amount); $ sth-> execute(); ' 但它仍然給我完全相同的錯誤。 – user2879055 2014-11-05 16:18:41