How can PHP developers leverage mysqli_fetch_all for improved array handling in their code?

PHP developers can leverage mysqli_fetch_all to retrieve all rows from a result set as an associative array, which simplifies array handling in their code. This function eliminates the need to loop through each row individually using mysqli_fetch_assoc, making the code more concise and efficient.

// Connect to the database
$connection = mysqli_connect("localhost", "username", "password", "database");

// Query the database
$result = mysqli_query($connection, "SELECT * FROM table");

// Fetch all rows as an associative array
$rows = mysqli_fetch_all($result, MYSQLI_ASSOC);

// Loop through the array and do something with each row
foreach ($rows as $row) {
    // Do something with $row
}

// Free the result set
mysqli_free_result($result);

// Close the connection
mysqli_close($connection);