本文介紹了在 OOP MySQLi 中獲取所有沒有循環的對象的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
這是我如何使用 MySQLi 獲取一條記錄:
This is how I get one record with MySQLi:
$result = $db->query("...");
$image = $result->fetch_object();
現在我需要獲取評論并將其傳遞給視圖.我現在正在這樣做,但似乎不對:
Now I need to get the comments and pass it to the view. I'm doing this right now but it doesn't seem right:
$result = $db->query("...");
while ($row = $result->fetch_object())
$comments[] = $row;
我想知道是否有辦法消除循環?像有 $image = $result->fetch_object(((s))),所以我的代碼看起來像:
I'm wondering if there's a way to remove the loop? Something like have $image = $result->fetch_object(((s))), so my code would look like:
$result = $db->query("...");
$comments = $result->fetch_objects();
推薦答案
是的.mysqli_result 類提供了一個 fetch_all 方法來做到這一點.但是,該方法只會返回關聯或數字數組(或混合數組),而不返回對象.
Yes. The mysqli_result class provides a fetch_all method to do this. However, that method will only return associative or numeric arrays (or a hybrid), not objects.
這篇關于在 OOP MySQLi 中獲取所有沒有循環的對象的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!