try { // Perform some operation on the database } catch (PDOException $e) { // Handle database errors $errorInfo = $e->errorInfo(); echo "Error code: " . $errorInfo[0] . "
"; echo "Error message: " . $errorInfo[2]; }
$pdo = new PDO("mysql:host=localhost;dbname=mydatabase", "username", "password"); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); try { // Perform some operation on the database } catch (PDOException $e) { // Handle database errors $errorInfo = $pdo->errorInfo(); echo "Error code: " . $errorInfo[0] . "In this example, we create a new PDO object to connect to a MySQL database and set the error mode to `ERRMODE_EXCEPTION`. We then perform an operation on the database and catch any database errors using a `try-catch` block. If an error occurs, we retrieve the error information from the PDO object using the `errorInfo` method and display the error code and message. The `errorInfo` object and method are part of the `PDO` package library in PHP.
"; echo "Error message: " . $errorInfo[2]; }