How can error handling be improved in PHP when executing database queries, to provide more informative messages to the user?

When executing database queries in PHP, error handling can be improved by using try-catch blocks to catch exceptions thrown by the database connection and query execution. Within the catch block, informative error messages can be displayed to the user, indicating the specific issue encountered during the query execution.

try {
    // Database connection code
    
    // Database query execution code
    
} catch (PDOException $e) {
    echo "Database error: " . $e->getMessage();
}