我只是想学习准备好的声明,我正在按照PHP手册指导我,我已经检查了有关stackoverflow上这个问题的答案但是,我找不到任何解决方案,$ stmt-> num_rows总是(返回0)
stackoverflow上有一篇文章讨论了这个问题,他们建议使用 $ stmt-> store_result()就在$ stmt-num_rows之前,但$ stmt-> num_rows返回0
有些人请告诉我这里我做错了什么....我只是厌倦了程序式编码,我想用准备好的声明提高我的技能
这是下面的功能
function get_all()
{
// ** Initializing the Connection
$mysqli = Connect();
$sql = ( ' SELECT * FROM `users` ' );
$stmt = $mysqli->prepare($sql);
$stmt->execute();
$res = $stmt->get_result();
echo $num_count = $stmt->num_rows();
$user = array();
for ($counter = 0; $row = $res->fetch_assoc(); $counter++)
{
$user[$counter] = $row;
}
return $user;
}
//这是第二次更新
function get_all()
{
// ** Initializing the Connection
$mysqli = Connect();
$sql = ( ' SELECT * FROM `users` ' );
$stmt = $mysqli->prepare($sql);
$stmt->execute();
$res = $stmt->get_result();
echo $num_count = $stmt->num_rows;
$user = array();
while($row = $res->fetch_assoc())
{
$user[] = $row;
}
return $user;
}
//第三次更新
function get_alll()
{
// ** Initializing the Connection
$mysqli = Connect();
// no need to use * character,
// need to write query this way
$sql = ( ' SELECT `id`,`fname`,`lname`,`uname`,`email` FROM `users` ' );
$stmt = $mysqli->prepare($sql);
// here need to use bind param
$stmt->bind_result( $id, $fname, $lname, $uname, $email);
$stmt->execute();
// it's important to store the result
// before using num rows
$res = $stmt->store_result();
echo $num_count = $stmt->num_rows;
//
while($stmt->fetch())
{
echo $fname;
}
}
答案 0 :(得分:3)
num_rows
是属性,不是方法,请使用不带括号的$stmt->num_rows