Get all objects without loop in OOP MySQLi

后端 未结 2 1486
遇见更好的自我
遇见更好的自我 2021-01-18 20:57

This is how I get one record with MySQLi:

$result = $db->query(\"...\");
$image = $result->fetch_object();

Now I need to get the comm

2条回答
  •  礼貌的吻别
    2021-01-18 21:46

    Without seeing your SQL, it's tough to say. There may be a better query you could use. Post your SQL and I'll take another look.

    In terms of your SQL query, if your query returns multiple rows, then you have already fetched them with one db call.

    I don't see a way to collect into an array all of the comments, but you can clean up your code with a custom function.

    function get_all_rows_as_array(&$result)
    {
        foreach($result as mysql_fetch_assoc($result))
        {
            $array[] = $row;
        }
    
        return $array;
    }
    
    $result = $db->query("...");
    $comments = get_all_rows_as_array($result);
    

提交回复
热议问题