PHP Tutorial
PHP Flow Control
PHP Functions
PHP String
PHP Array
PHP Date Time
PHP Object Oriented
Regular Expression
PHP Cookie & Session
PHP Error & Exception handling
MySQL in PHP
PHP File Directory
PHP Image Processing
The errorCode()
and errorInfo()
methods are part of the PDO (PHP Data Objects) class in PHP, which is used to interact with databases. These methods are used to get information about errors that occur when interacting with a database.
errorCode()
The errorCode()
method is used to fetch the SQLSTATE associated with the last operation on the database handle.
The SQLSTATE is a standard set of error codes for SQL. It's a five characters long alphanumeric identifier defined in the ANSI SQL standard. The first two characters define the class of the error (e.g., "42" is for syntax errors) and the last three define the subclass (e.g., "000" means "no further subclass").
Here is an example of its usage:
<?php $pdo = new PDO("mysql:host=localhost;dbname=test", "root", ""); $stmt = $pdo->prepare('SELECT * FROM non_existent_table'); if (!$stmt->execute()) { echo $pdo->errorCode(); }
In this example, we're trying to select data from a table that does not exist. The execute
method will return false
and errorCode
will print an error code.
errorInfo()
The errorInfo()
method returns an array of error information about the last operation performed by this database handle. The array consists of the following fields:
Here is an example of its usage:
<?php $pdo = new PDO("mysql:host=localhost;dbname=test", "root", ""); $stmt = $pdo->prepare('SELECT * FROM non_existent_table'); if (!$stmt->execute()) { $errorInfo = $pdo->errorInfo(); print_r($errorInfo); }
In this example, we're trying to select data from a table that does not exist. The execute
method will return false
and errorInfo
will print an array with the SQLSTATE error code, MySQL error code, and MySQL error message.
Remember, these methods will only provide useful information if the last operation on the database handle resulted in an error. If the last operation was successful, these methods may not return useful information.
Handling errors and exceptions in PHP PDO:
try { // PDO code here } catch (PDOException $e) { echo "Error: " . $e->getMessage(); }
Retrieving error codes with errorCode()
in PDO:
errorCode()
returns the SQLSTATE error code.$stmt = $pdo->prepare("SELECT * FROM non_existent_table"); $stmt->execute(); $errorCode = $stmt->errorCode(); echo "Error code: $errorCode";
Getting detailed error information with errorInfo()
in PDO:
errorInfo()
returns an array with error information.$stmt = $pdo->prepare("SELECT * FROM non_existent_table"); $stmt->execute(); $errorInfo = $stmt->errorInfo(); echo "Error info: " . implode(", ", $errorInfo);
Interpreting PDO error codes and messages in PHP:
$stmt = $pdo->prepare("SELECT * FROM non_existent_table"); $stmt->execute(); $errorInfo = $stmt->errorInfo(); echo "SQLSTATE: {$errorInfo[0]}, Code: {$errorInfo[1]}, Message: {$errorInfo[2]}";
Custom error handling strategies with PDO in PHP:
set_exception_handler()
.function customExceptionHandler($exception) { echo "Custom Exception Handler: " . $exception->getMessage(); } set_exception_handler('customExceptionHandler'); try { // PDO code here } catch (PDOException $e) { // Exception handled by custom handler }
Logging and recording PDO errors with errorCode()
and errorInfo()
:
$stmt = $pdo->prepare("SELECT * FROM non_existent_table"); $stmt->execute(); $errorCode = $stmt->errorCode(); $errorInfo = $stmt->errorInfo(); // Log error information error_log("SQLSTATE: $errorCode, Code: {$errorInfo[1]}, Message: {$errorInfo[2]}");
Handling transaction errors using PDO error functions:
try { $pdo->beginTransaction(); // SQL queries here $pdo->commit(); } catch (PDOException $e) { $pdo->rollBack(); echo "Transaction failed: " . $e->getMessage(); }
Displaying user-friendly error messages in PDO:
try { // PDO code here } catch (PDOException $e) { echo "An error occurred. Please try again later."; error_log("PDO Error: " . $e->getMessage()); }