【发布时间】:2011-02-25 14:03:56
【问题描述】:
我有以下脚本:
<?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。有人对此有解释吗?在我看来,一旦您将超过 1 个参数绑定到语句,num_rows 就会停止工作。
p.s:在完整的脚本中,这里使用 call_user_func_array 是有原因的,而不是使用 call_user_func_array 会得到相同的结果。
【问题讨论】:
标签: php mysql mysqli prepared-statement