How can error handling be improved when dealing with multiple queries in PHP?

When dealing with multiple queries in PHP, error handling can be improved by using try-catch blocks to catch exceptions that may arise during the execution of the queries. This allows for more granular control over error handling and provides the opportunity to handle errors in a specific and tailored manner.

try {
    // First query
    $result1 = mysqli_query($connection, "SELECT * FROM table1");
    
    // Second query
    $result2 = mysqli_query($connection, "SELECT * FROM table2");
    
    // Check for errors in each query
    if(!$result1) {
        throw new Exception("Error executing query 1: " . mysqli_error($connection));
    }
    
    if(!$result2) {
        throw new Exception("Error executing query 2: " . mysqli_error($connection));
    }
    
    // Process results of both queries
    // ...
    
} catch (Exception $e) {
    echo "Error: " . $e->getMessage();
}