PHP $ stmt-> num_rows在准备好的语句中不起作用 [英] PHP $stmt->num_rows doesnt work by prepared statements
本文介绍了PHP $ stmt-> num_rows在准备好的语句中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要检查if ($numRows >= 1)
,然后它应该返回一些内容.
I want to check if ($numRows >= 1)
then it should return something.
当我使用$con->mysql("{QUERY}")
时,它可以工作.
When I use $con->mysql("{QUERY}")
, it works.
但是当我使用$stmt = $con->prepare("{QUERY}")
时,它不起作用.
But when I use $stmt = $con->prepare("{QUERY}")
, it doesn't work.
有人知道吗?
<?php
if ($result = $con->query("SELECT username FROM users WHERE username = 'test'")) {
$numRows = $result->num_rows;
echo $numRows;
}
?>
结果:1
<?php
$name = 'test';
$stmt = $con->prepare("SELECT username FROM users WHERE username = ?");
$stmt->bind_param('s', $name);
$name = 'test';
$stmt->execute();
$numRows = $stmt->num_rows;
echo $numRows;
?>
结果:0
推荐答案
在调用->num_rows()
方法之前,您需要从SELECT
查询中转移结果集.
You need to transfer the result set from the SELECT
query before calling the ->num_rows()
method.
// your code
$stmt->execute();
$stmt->store_result();
$numRows = $stmt->num_rows;
echo $numRows;
这里是参考:
Here's the reference:
这篇关于PHP $ stmt-> num_rows在准备好的语句中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文