2013-06-18 38 views
-1

我遇到了PDO問題,我已經看到過,但提供的不同解決方案對我無效。綁定數字以設置SQL查詢的LIMIT時。使用PHP綁定PDO查詢的限制數

這裏是我的錯誤:

Warning: PDOStatement::execute(): SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''2'' at line 1 in ... 

代碼:

$remaining = 3 - $countRows; 
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); 
$result4 = $con->prepare("SELECT * FROM item_descr WHERE id_item != ? LIMIT ?"); 
$result4->execute(array($itemId, intval($remaining))); 

回答

1

你爲什麼不這樣做:

SELECT * FROM item_descr WHERE id_item != :id LIMIT :limit 

$result->bindParam(':id', $itemID, PDO::PARAM_INT); 
$result->bindParam(':limit', $remainint, PDO::PARAM_INT); 

與您現有的代碼,你是不是真的綁定參數。

+0

這不起作用:致命錯誤:調用一個成員函數bindParam()在非中的對象 – samyb8