What are some best practices for handling exceptions and error messages in PHP database operations to effectively debug and resolve issues with query execution?

When working with PHP database operations, it is crucial to handle exceptions and error messages effectively to debug and resolve issues with query execution. One best practice is to use try-catch blocks to catch exceptions thrown by database operations and display meaningful error messages to aid in troubleshooting.

try {
    // Perform database query
    $result = $pdo->query("SELECT * FROM table");
    
    // Process query result
    foreach ($result as $row) {
        // Process each row
    }
} catch (PDOException $e) {
    // Handle database query exception
    echo "Database query failed: " . $e->getMessage();
}