为什么mysqli num_rows总是返回0? [英] Why does mysqli num_rows always return 0?
本文介绍了为什么mysqli num_rows总是返回0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直无法使用mysqli获取要返回的行数.即使肯定有一些结果,我每次都会得到0.
I've been having trouble getting the number of rows to return using mysqli. I just get 0 back every time even though there are definitely some results.
if($stmt = $mysqli->prepare("SELECT id, title, visible, parent_id FROM content WHERE parent_id = ? ORDER BY page_order ASC;")){
$stmt->bind_param('s', $data->id);
$stmt->execute();
$num_of_rows = $stmt->num_rows;
$stmt->bind_result($child_id, $child_title, $child_visible, $child_parent);
while($stmt->fetch()){
//code
}
echo($num_of_rows);
$stmt->close();
}
为什么没有显示正确的数字?
Why doesn't it show the correct number?
推荐答案
您需要调用 MySqli_Stmt::store_result()
在num_rows查找之前:
You need to call MySqli_Stmt::store_result()
prior to the num_rows lookup:
if($stmt = $mysqli->prepare("SELECT id, title, visible, parent_id FROM content WHERE parent_id = ? ORDER BY page_order ASC;")){
$stmt->bind_param('s', $data->id);
$stmt->execute();
$stmt->store_result(); <-- This needs to be called here!
$num_of_rows = $stmt->num_rows;
$stmt->bind_result($child_id, $child_title, $child_visible, $child_parent);
while($stmt->fetch()){
//code
}
echo($num_of_rows);
$stmt->close();
}
请参见 MySQLi_Stmt->num_rows
上的文档,它说它就在页面顶部附近(在主要描述块中)...
See the docs on MySQLi_Stmt->num_rows
, it says it right near the top of the page (in the main description block)...
这篇关于为什么mysqli num_rows总是返回0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文