How can one ensure that each result row from a database query in PHP is correctly stored as an object with unique properties within an array?

When fetching results from a database query in PHP, you can ensure that each result row is correctly stored as an object with unique properties within an array by creating a new object for each row and then adding that object to an array. This way, each row will be represented as a separate object with its own unique properties.

// Assuming $result is the result set from a database query

$results_array = array();

while ($row = $result->fetch_assoc()) {
    $result_object = new stdClass();
    
    foreach ($row as $key => $value) {
        $result_object->$key = $value;
    }
    
    $results_array[] = $result_object;
}

// Now $results_array contains each row from the query result as an object with unique properties