How can a PHP developer handle database query results that are not in array format and avoid errors when using foreach loops for iteration?
When database query results are not in array format, a PHP developer can handle them by first checking if the result is iterable using the `is_iterable()` function. If it is not iterable, the developer can convert it to an array using `iterator_to_array()` before using a `foreach` loop for iteration. This approach helps avoid errors when trying to iterate over non-array data structures.
// Assuming $result is the database query result
if (is_iterable($result)) {
foreach ($result as $row) {
// Process each row here
}
} else {
$resultArray = iterator_to_array($result);
foreach ($resultArray as $row) {
// Process each row here
}
}