How can error reporting be optimized in PHP to debug potential issues with database queries?

To optimize error reporting in PHP for debugging potential issues with database queries, you can set the error reporting level to include warnings and errors related to database operations. This can help identify issues such as syntax errors, connection problems, or query execution problems. Additionally, you can utilize try-catch blocks to catch and handle exceptions that may occur during database operations.

// Set error reporting level to include warnings and errors
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Example database connection and query execution
try {
    $pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');
    $stmt = $pdo->query('SELECT * FROM mytable');
} catch (PDOException $e) {
    echo 'Error: ' . $e->getMessage();
}