How can the mysqli_fetch_all() function be used to simplify data retrieval from a MySQL database in PHP?

Using the mysqli_fetch_all() function in PHP can simplify data retrieval from a MySQL database by fetching all rows of a result set as an associative array, allowing for easier access and manipulation of the data. This eliminates the need to loop through each row individually using mysqli_fetch_assoc() or mysqli_fetch_array().

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

// Query to retrieve data from database
$query = "SELECT * FROM table";
$result = mysqli_query($connection, $query);

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

// Loop through the data
foreach ($data as $row) {
    // Access data using column names
    echo $row['column_name'] . "<br>";
}

// Close connection
mysqli_close($connection);