How can error handling be implemented when fetching data from a MySQL query into arrays in PHP?

When fetching data from a MySQL query into arrays in PHP, error handling can be implemented by checking for errors after executing the query using functions like mysqli_error(). This allows you to handle any errors that may occur during the data retrieval process.

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

// Check for errors
if (!$result) {
    die("Error: " . mysqli_error($connection));
}

// Fetch data into an array
$data = mysqli_fetch_all($result, MYSQLI_ASSOC);

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