[英]zend framework: how to prepare and execute WHERE IN clause?
我想准备一个在循环内使用的语句。 当我尝试执行该语句时,我在日志中看到一条错误消息,显示“参数编号无效:未绑定参数”。
我的代码有什么问题?
$itemSelectSql = "SELECT * FROM `tblItems` WHERE `itemID` IN (?)";
$itemSelectStmt = new Zend_Db_Statement_Mysqli($this->db_ro, $itemSelectSql);
while () {
...
$itemIds = array();
// populate $itemIds array
...
$itemSelectStmt->execute(array($itemIds));
}
编辑:
我想我的设置中可能有一个错误,这解释了为什么我尝试失败的原因。 我看到了这个:
PHP Warning: call_user_func_array() expects parameter 1 to be a valid callback,
class 'PDOStatement' does not have a method 'bind_param' in
/var/www/lib/Zend/Db/Statement/Mysqli.php on line 204
编辑:
我使用了错误的适配器。 应该是 Zend_Db_Statement_Pdo :-)
感谢您的回复。
?
不能用数组替换,它必须用标量替换(感谢评论指出这并不总是意味着字符串......我的大脑放屁)。 让我知道这是否效果更好:
$itemSelectSql = "SELECT * FROM `tblItems` WHERE `itemID` IN ";
while () {
...
$itemIds = array();
// populate $itemIds array
...
// we need to have the same number of "?,"'s as there are items in the array.
// and then remove final comma.
$qs = rtrim(str_repeat("?,", count($itemIds)),',');
// create a statement based on the result
$itemSelectStmt =
new Zend_Db_Statement_Mysqli($this->db_ro, "$itemSelectSql ($qs)");
// bind to each of those commas.
$itemSelectStmt->execute($itemIds);
}
您是否尝试过这样做:
while () {
...
$itemIds = array();
// populate $itemIds array
$itemIds = implode(',' $itemIds);
$itemSelectStmt->execute(array($itemIds));
}
我不是 Zend_framework 的专家,但是当你使用语句时,你必须
如果您使用位置参数,或那些标有问号 ('?') 的参数,请将绑定值传递到普通数组中。
所以我认为你需要传递一个带有一个值的数组,并且该值替换“?” 在声明中。 在这种情况下,您需要一个逗号分隔的字符串(如果您的 id 是 int)来替换语句中的 (?)。
如果您不内爆这些值,您所做的就是传递一个包含数组的数组。
您可以使用FIND_IN_SET(str,strlist)
而不是IN ()
:
mysql> select id, name from customer where find_in_set(id, '10,15,20');
+----+---------+
| id | name |
+----+---------+
| 10 | ten |
| 15 | fifteen |
| 20 | twelve |
+----+---------+
这样,您不必将具有多个值的数组绑定到IN()
,您可以将 ID 列表作为单个逗号分隔的字符串传递。 您可以安全地使用 PHP function implode()
从 ID 数组生成字符串。
不过,我不确定它是否对性能有任何影响。 我查看了explain
的 output ,看起来find_in_set()
无法使用索引,因此生成具有可变数量参数的查询应该会更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.