What are some best practices for handling errors related to MySQL queries in PHP, such as undefined function errors or invalid result resource warnings?

When handling errors related to MySQL queries in PHP, it is important to check for errors after executing the query and handle them appropriately. This can include checking for undefined function errors or invalid result resource warnings. One common approach is to use the `mysqli_error()` function to retrieve the error message and log it or display it to the user for debugging purposes.

// Execute the MySQL query
$result = mysqli_query($connection, $query);

// Check for errors
if (!$result) {
    $error_message = mysqli_error($connection);
    // Log or display the error message
    echo "Error: " . $error_message;
} else {
    // Process the query result
    while ($row = mysqli_fetch_assoc($result)) {
        // Do something with the data
    }
}