PHP PDO::errorInfo Method
last modified April 19, 2025
The PDO::errorInfo method in PHP provides detailed error information about the last operation performed by a PDO database handle. It returns an array.
Basic Definition
PDO::errorInfo fetches extended error information associated with the last operation on the database handle. It returns an array of error information.
Syntax: public PDO::errorInfo(): array. The array contains three
fields: SQLSTATE error code, driver-specific error code, and error message.
Basic errorInfo Example
This shows a simple example of using errorInfo after a failed query.
<?php
declare(strict_types=1);
try {
$pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
$stmt = $pdo->query('SELECT * FROM non_existent_table');
if ($stmt === false) {
$error = $pdo->errorInfo();
echo "SQLSTATE: {$error[0]}\n";
echo "Driver Code: {$error[1]}\n";
echo "Error Message: {$error[2]}\n";
}
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
This example attempts to query a non-existent table. With ERRMODE_SILENT, errors don't throw exceptions. errorInfo provides detailed error information.
errorInfo with Prepared Statements
This demonstrates using errorInfo with prepared statement execution errors.
<?php
declare(strict_types=1);
try {
$pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
$stmt = $pdo->prepare('INSERT INTO users (name, email) VALUES (?, ?)');
$result = $stmt->execute(['John Doe', 'invalid-email']);
if ($result === false) {
$error = $stmt->errorInfo();
echo "Error: {$error[2]}\n";
echo "SQLSTATE: {$error[0]}\n";
} else {
echo "Record inserted successfully\n";
}
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
This shows errorInfo usage with a prepared statement. If execute fails, errorInfo provides details. Note we use the statement's errorInfo method.
Comparing errorInfo and errorCode
This example compares errorInfo with the simpler errorCode method.
<?php
declare(strict_types=1);
try {
$pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
$result = $pdo->exec('DELETE FROM non_existent_table');
if ($result === false) {
echo "errorCode: " . $pdo->errorCode() . "\n";
$errorInfo = $pdo->errorInfo();
echo "Full error info:\n";
print_r($errorInfo);
}
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
This shows the difference between errorCode and errorInfo. errorCode returns just the SQLSTATE while errorInfo provides complete error details in an array.
errorInfo with Transactions
This demonstrates using errorInfo when a transaction fails.
<?php
declare(strict_types=1);
try {
$pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
$pdo->beginTransaction();
$result1 = $pdo->exec("UPDATE accounts SET balance = balance - 100 WHERE user_id = 1");
$result2 = $pdo->exec("UPDATE non_existent_table SET balance = balance + 100 WHERE user_id = 2");
if ($result1 === false || $result2 === false) {
$pdo->rollBack();
$error = $pdo->errorInfo();
echo "Transaction failed: {$error[2]}\n";
} else {
$pdo->commit();
echo "Transaction completed successfully\n";
}
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
This shows errorInfo usage in a transaction context. When one query fails, we roll back and use errorInfo to get details about what went wrong.
Database-Specific Error Codes
This example shows how errorInfo returns driver-specific error codes.
<?php
declare(strict_types=1);
try {
$pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
// Attempt to violate a unique constraint
$result = $pdo->exec("INSERT INTO users (email) VALUES ('duplicate@example.com')");
if ($result === false) {
$error = $pdo->errorInfo();
echo "MySQL Error Code: {$error[1]}\n";
echo "Error Message: {$error[2]}\n";
// MySQL specific error code for duplicate entry
if ($error[1] == 1062) {
echo "Duplicate entry detected!\n";
}
}
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
This demonstrates how to use the driver-specific error code (array index 1) to detect specific error conditions like duplicate entries in MySQL.
errorInfo with Different Databases
This shows how errorInfo works with SQLite versus MySQL.
<?php
declare(strict_types=1);
// MySQL example
try {
$mysql = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password');
$mysql->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
$result = $mysql->exec("INVALID SQL");
if ($result === false) {
echo "MySQL Error:\n";
print_r($mysql->errorInfo());
}
} catch (PDOException $e) {
echo "MySQL Error: " . $e->getMessage();
}
// SQLite example
try {
$sqlite = new PDO('sqlite:/path/to/database.sqlite');
$sqlite->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
$result = $sqlite->exec("INVALID SQL");
if ($result === false) {
echo "\nSQLite Error:\n";
print_r($sqlite->errorInfo());
}
} catch (PDOException $e) {
echo "SQLite Error: " . $e->getMessage();
}
This shows how errorInfo works across different database systems. While the array structure is consistent, the specific codes and messages vary by driver.
Best Practices for errorInfo
- Check for Errors: Always verify operations succeeded before calling errorInfo.
- Use with ERRMODE_SILENT: errorInfo is most useful in silent error mode.
- Statement vs Connection: Call errorInfo on the appropriate object (statement or connection).
- Log Full Details: For debugging, log the entire errorInfo array.
- User-Friendly Messages: Don't expose raw errorInfo to users.
Source
PHP PDO::errorInfo Documentation
This tutorial covered the PDO::errorInfo method with practical examples showing how to retrieve and interpret database error information in PHP.
Author
List all PHP PDO Functions.