How can the output of a MySQL query be checked for errors in a PHP script?

To check for errors in the output of a MySQL query in a PHP script, you can use the mysqli_error() function to retrieve any error messages generated by the most recent MySQL operation. By checking if mysqli_error() returns an empty string, you can determine if the query was successful or if there was an error.

// Perform a MySQL query
$result = mysqli_query($connection, "SELECT * FROM table");

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

// Process the query result
while($row = mysqli_fetch_assoc($result)) {
    // Do something with the data
}

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