为什么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).
相关文章