php - zend 框架 : how to prepare and execute WHERE IN clause?

标签 php zend-framework pdo

我想准备一个在循环内使用的语句。当我尝试执行该语句时,我在日志中看到一条错误消息,提示“参数编号无效:未绑定(bind)任何参数”。

我的代码有什么问题?

$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);
}

关于php - zend 框架 : how to prepare and execute WHERE IN clause?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6305738/

相关文章:

php - 我想使用正则表达式匹配字符串的第一个世界

php - 使用 PHP 扩展从 PHP 连接 C 套接字服务器

php - 将 CSV 导入 MySQL 时保留小数位

javascript - 通过 ajax 登录和注册是否安全?

php - Zend 表单 : How to pass parameters into the constructor?

php - MySQL:多条件查询

php - 使用 PHP/MySql/PDO 更新除自增字段以外的所有值

PHP查询选择一条记录

javascript - PHP stringID 未从 echo 中读取 onclick

php - 使用 zend 框架进行多个子查询