How can PHP developers improve error handling when encountering syntax issues in database queries?

When encountering syntax issues in database queries, PHP developers can improve error handling by utilizing try-catch blocks to catch exceptions thrown by the database query execution. This allows developers to handle errors gracefully and provide meaningful error messages to troubleshoot the issue effectively.

try {
    // Database connection code

    // Database query with potential syntax issue
    $query = "SELECT * FROM users WHERE username = 'john'";

    // Execute the query
    $result = $pdo->query($query);

    // Fetch data from the result
    while ($row = $result->fetch()) {
        // Process the data
    }

} catch (PDOException $e) {
    // Handle the exception
    echo "Error executing query: " . $e->getMessage();
}