How can error handling be improved in PHP when executing SQL queries to identify and troubleshoot issues more effectively?

When executing SQL queries in PHP, error handling can be improved by utilizing try-catch blocks to catch exceptions and display detailed error messages. This can help identify and troubleshoot issues more effectively by providing specific information about what went wrong during the query execution.

try {
    // Your SQL query execution code here
    $result = $pdo->query("SELECT * FROM table");
} catch (PDOException $e) {
    // Display detailed error message
    echo "Error executing query: " . $e->getMessage();
}