作者:隔壁老吴 | 来源:互联网 | 2023-05-16 21:51
IwaswritingadatabasehandlerclassinPHPusingthemysqliclassandpreparedstatements.Iwas
I was writing a database handler class in PHP using the mysqli class and prepared statements. 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 test code to keep it simpler so I could see what was going wrong. 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. Here is the code:
我正在使用mysqli类和预处理语句在PHP中编写数据库处理程序类。我试图打印出结果。它没有正常工作,所以我决定做一些调试。我试图使用mysqli_statement类中的num_rows()方法,但它一直返回0.我决定编写一小部分测试代码以使其更简单,以便我可以看到出了什么问题。然后我能够返回我想要的数据,但即使实际选择和检索某些数据,num_rows()方法仍然返回0。这是代码:
$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();
}
So yeah, expected result is:
所以是的,预期的结果是:
1name
And actual result is:
实际结果是:
0name
Can anyone tell me why this is?
谁能告诉我为什么会这样?
3 个解决方案