我有以下脚本:
<?php
$mysqli = new mysqli('localhost', 'user', 'password', 'database');
$statement = $mysqli->stmt_init();
$query = 'SELECT * FROM table WHERE id = ? AND active = 1';
$statement->prepare($query);
$parameters = array('i');
$inputParameters = array(10);
foreach ($inputParameters as $param) {
$parameters[] =& $param;
}
call_user_func_array(array($statement, 'bind_param'), $parameters);
$statement->execute();
$statement->store_result();
echo $statement->num_rows;
?>
这会返回正确的行数。
但是当我将脚本更改为:
<?php
$mysqli = new mysqli('localhost', 'user', 'password', 'database');
$statement = $mysqli->stmt_init();
$query = 'SELECT * FROM table WHERE id = ? AND active = ?';
$statement->prepare($query);
$parameters = array('ii');
$inputParameters = array(10, 1);
foreach ($inputParameters as $param) {
$parameters[] =& $param;
}
call_user_func_array(array($statement, 'bind_param'), $parameters);
$statement->execute();
$statement->store_result();
echo $statement->num_rows;
?>
它返回 0。有人对此有解释吗?对我来说,一旦语句绑定(bind)了超过 1 个参数,num_rows 就会停止工作。
p.s:在完整的脚本中,有一个使用 call_user_func_array
的原因,不使用 call_user_func_array
会得到相同的结果。
最佳答案
经过大量调试后我找到了答案:第二个代码中的 $parameters
将是 array('ii', 1, 1)
。这是因为那里使用的引用。将 foreach ($inputParameters as $param) {
更改为 foreach ($inputParameters as &$param) {
解决了问题
关于php - Mysqli 准备语句 num_rows 带有多个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5118128/