How can error reporting be optimized in PHP to quickly identify and resolve issues in MySQL queries?

To optimize error reporting in PHP for MySQL queries, you can enable error reporting, set the error mode to exception, and handle errors gracefully by catching exceptions. This will help quickly identify and resolve any issues that may arise during MySQL query execution.

// Enable error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Set error mode to exception
$dbh = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// Execute MySQL query
try {
    $stmt = $dbh->query('SELECT * FROM mytable');
    // Process query results
} catch (PDOException $e) {
    echo 'Error: ' . $e->getMessage();
}