![](/img/trans.png)
[英]Using PHP mysqli: where clause in bind_param might be NULL
[英]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.