绑定数到PHP的PDO查询的LIMIT


Binding number to the LIMIT of a PDO query with PHP

我遇到了PDO的问题,我已经看到了周围的讨论,但提供的不同解决方案不适合我。当绑定一个数字来设置SQL查询的LIMIT时,

下面是我得到的错误:

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)));
$dbh->setAttribute( PDO::ATTR_EMULATE_PREPARES, false );

添加这一行并删除intval(是解决方案,如下所示:

类似问题的答案

你为什么不这样做呢?

SELECT  * FROM  item_descr WHERE id_item != :id LIMIT :limit
$result->bindParam(':id', $itemID, PDO::PARAM_INT);
$result->bindParam(':limit', $remainint, PDO::PARAM_INT);

在你当前的代码中,你并没有真正绑定参数