What are the potential benefits and drawbacks of using a foreach loop in PHP for database queries?

When using a foreach loop in PHP for database queries, one potential benefit is that it allows you to easily iterate over the results of a query and process each row individually. However, a drawback is that it may not be the most efficient way to handle large result sets, as it loads all the data into memory at once. In such cases, it may be better to use a while loop to fetch and process rows one at a time.

// Example of using a foreach loop for database queries
$query = "SELECT * FROM users";
$result = $mysqli->query($query);

if ($result->num_rows > 0) {
    foreach ($result as $row) {
        // Process each row here
        echo $row['username'] . "<br>";
    }
}