Get all objects without loop in OOP MySQLi

后端 未结 2 1489
遇见更好的自我
遇见更好的自我 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:31

    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.

    0 讨论(0)
  • 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);
    
    0 讨论(0)
提交回复
热议问题