Skip to content
PHP Logo

How can the error information retrieved from PDO be effectively utilized and displayed in PHP scripts for debugging purposes?

When retrieving error information from PDO in PHP scripts for debugging purposes, it is important to utilize the `errorInfo()` method to access detailed error messages. This method returns an array containing error information such as SQLSTATE error code, driver-specific error code, and error message. By accessing and displaying this error information, developers can effectively identify and troubleshoot issues in their database queries.

php
// Example code snippet to retrieve and display PDO error information for debugging
try {
    // PDO database connection code here
    
    // Database query code here
    
} catch (PDOException $e) {
    $errorInfo = $e->errorInfo();
    
    echo "SQLSTATE error code: " . $errorInfo[0] . "<br>";
    echo "Driver-specific error code: " . $errorInfo[1] . "<br>";
    echo "Error message: " . $errorInfo[2] . "<br>";
}