How can the fetch_all() function in PHP be utilized to avoid overwriting data in an array when fetching results from a MySQL query?

When fetching results from a MySQL query in PHP using fetch_all(), it is important to ensure that the fetched data does not overwrite existing data in the array. To avoid this issue, you can append the fetched results to the array instead of overwriting it. This can be achieved by using the array_merge() function to combine the existing array with the fetched results.

// Assuming $result is the result of a MySQL query
$data = array(); // Initialize an empty array to store the results

// Fetch all rows from the result set and append them to the $data array
while ($row = $result->fetch_assoc()) {
    $data[] = $row;
}

// Alternatively, you can use array_merge to combine the existing array with the fetched results
$data = array_merge($data, $result->fetch_all(MYSQLI_ASSOC));