本文介紹了為什么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?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!