繁体   English   中英

使用PHP 5.3+为“ WHERE IN”语句动态生成mysqli bind_param

[英]Dynamically generating mysqli bind_param for “WHERE IN” statements using php 5.3+

这个问题依赖于有关使用call_user_func_array动态生成bind_param语句的答案 ,以及在php手册上发布的有关必须将引用的项目传递给bind_param语句的用户答案 但是,问题仍然存在:

PHP警告:mysqli_stmt :: bind_param():变量数量与准备好的语句中的参数数量不匹配

这是代码:

$error_id_list = implode(',', array_fill(0, count($error_ids), '?'));
$type=""; for ($i=0; $i<count($error_ids); $i++){$type .= "i";}
$query = "SELECT `id`, `question`, `multi_1`, `multi_2`, `multi_3`, `multi_4` FROM `student_exam` WHERE `id` IN ('".$error_id_list."') ORDER BY RAND() LIMIT 0, 5";
$result = $mysqli->prepare($query);
if($result === FALSE)
    die($result->error);
array_unshift($error_ids, $type);
call_user_func_array(array($result, 'bind_param'), refValues($error_ids));
$result->execute();
$result->store_result();
$result->bind_result($id,$question,$multi1,$multi2,$multi3,$multi4);

while($result->fetch()){
    $output;
}
$result->free_result();

return $output;

谢谢!

该错误是微不足道的,SQL语句中的问号不需要用引号引起来(尽管StackOverflow上的其他几个答案都使用了引号)。

$error_id_list = implode(',', array_fill(0, count($error_ids), '?'));
$type=""; for ($i=0; $i<count($error_ids); $i++){$type .= "i";}
$query = "SELECT `id`, `question`, `multi_1`, `multi_2`, `multi_3`, `multi_4` FROM 
  `student_exam` WHERE `id` IN ('".$error_id_list."') ORDER BY RAND() LIMIT 0, 5";
                                ^^--- no quotes --^^
$result = $mysqli->prepare($query);
if($result === FALSE)
    die($result->error);
array_unshift($error_ids, $type);
call_user_func_array(array($result, 'bind_param'), refValues($error_ids));
$result->execute();
$result->store_result();
$result->bind_result($id,$question,$multi1,$multi2,$multi3,$multi4);

while($result->fetch()){
    $output;
}
$result->free_result();

return $output;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM