What are best practices for error handling and debugging in PHP when encountering issues with database queries?

When encountering issues with database queries in PHP, it is important to implement proper error handling and debugging techniques to quickly identify and resolve the problem. One best practice is to use try-catch blocks to catch any exceptions thrown during the query execution and handle them appropriately. Additionally, enabling error reporting and logging can help in tracking down the root cause of the issue.

try {
    // Perform database query
    $result = $pdo->query("SELECT * FROM table_name");
    
    // Process query result
    foreach($result as $row) {
        // Do something with the data
    }
} catch (PDOException $e) {
    // Handle any exceptions thrown during query execution
    echo "Error executing query: " . $e->getMessage();
}