[英]mysqli_stmt::num_rows() returns the wrong value
I was writing a database handler class in PHP using the mysqli class and prepared statements.我正在使用 mysqli 类和准备好的语句在 PHP 中编写一个数据库处理程序类。 I was attempting to print out the result.我试图打印出结果。 It didn't work right off the bat so I decided to do some debugging.它没有立即起作用,所以我决定进行一些调试。 I tried to use the num_rows()
method from the mysqli_statement
class, but it kept returning 0. I decided to write a small portion of the test code to keep it simpler so I could see what was going wrong.我尝试使用mysqli_statement
类中的num_rows()
方法,但它一直返回 0。我决定编写一小部分测试代码以使其更简单,以便我可以看到出了什么问题。 I was then able to return the data I wanted, but the num_rows()
method still returns 0 even when it is actually selecting and retrieving some data.然后我能够返回我想要的数据,但是num_rows()
方法即使在实际选择和检索一些数据时仍然返回 0。 Here is the code:这是代码:
$mysqli = new mysqli('localhost', 'username', 'password', 'database');
if(mysqli_connect_errno())
{
die('connection failed');
}
$statement = $mysqli->stmt_init();
$query = "SELECT name FROM table WHERE id = '2000'";
if($statement->prepare($query))
{
$statement->execute();
$statement->bind_result($name);
$statement->fetch();
$statement->store_result();
echo $statement->num_rows();
echo $name;
}
else
{
echo 'prepare statement failed';
exit();
}
The expected result is:预期的结果是:
1name
And the actual result is:实际结果是:
0name
Can anyone tell me why this is?谁能告诉我这是为什么?
I wonder if num_rows() is reporting relative to the current resultset.我想知道 num_rows() 是否相对于当前结果集进行报告。 Try capturing num_rows() prior to fetching the data.在获取数据之前尝试捕获 num_rows()。 eg例如
if($statement->prepare($query))
{
$statement->execute();
$statement->store_result();
echo $statement->num_rows();
$statement->bind_result($name);
$statement->fetch();
echo $name;
}
Does that have any effect?那有什么影响吗?
num_rows
不是方法,而是属性。
In order to be able to use mysqli_stmt::num_rows(),
you need to fetch all rows into PHP.为了能够使用mysqli_stmt::num_rows(),
您需要将所有行提取到 PHP 中。 There are two ways to fetch everything: buffering using store_result()
or manual fetching of all rows using fetch()
.有两种方法可以获取所有内容:使用store_result()
缓冲或使用fetch()
手动获取所有行。
In your case, you have started manual fetching by calling fetch()
once.在您的情况下,您已经通过调用一次fetch()
开始手动提取。 You can't call store_result()
when another fetch process is ongoing.当另一个提取过程正在进行时,您不能调用store_result()
。 The call to store_result()
fails with an error*.调用store_result()
失败并出现错误*。
$statement->fetch();
$statement->store_result(); // produces error. See $mysqli->error;
echo $statement->num_rows();
The easiest solution is to swap the order in which you call these two methods.最简单的解决方案是交换调用这两个方法的顺序。
$statement->store_result();
$statement->fetch(); // This will initiate fetching from PHP buffer instead of MySQL buffer
echo $statement->num_rows(); // This will tell you the total number of rows fetched to PHP
* Due to a bug in PHP, this error will not trigger an exception in the exception error reporting mode. * 由于PHP的一个bug,这个错误在异常报错模式下不会触发异常。 The error message can only be seen with mysqli_error()
function or its corresponding property.错误信息只能通过mysqli_error()
函数或其相应的属性看到。
It doesn't look like you've declared $name.看起来您没有声明 $name。
Also, try removing bind_result() and fetch() so it reads something like this:此外,尝试删除 bind_result() 和 fetch() 使其读取如下内容:
$statement->execute();
$statement->store_result();
printf("Number of rows: %d.\n", $statement->num_rows);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.