Content · Web Development · PHP

PHP PDO::errorInfo() Method: Usage and Examples

Returns SQLSTATE, driver-specific error code and driver-specific error message for the last connection operation. When using exceptions, inspect caught PDOException rather than exposing database diagnostics to

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

Returns SQLSTATE, driver-specific error code and driver-specific error message for the last connection operation.

The objective is to understand the behavior and operational limits, not only the method name.

What It Does


Returns SQLSTATE, driver-specific error code and driver-specific error message for the last connection operation.

Syntax and Return Value


PDO::errorInfo(): mixed

The return value and exceptional cases depend on the call described here: When using exceptions, inspect caught PDOException rather than exposing database diagnostics to users.

Example 1: Basic Use


This self-contained SQLite example uses in-memory demo data; it requires the pdo_sqlite driver.

<?php
$pdo = new PDO('sqlite::memory:', null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$pdo->exec('CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT NOT NULL, price INTEGER NOT NULL, active INTEGER)');
$pdo->exec("INSERT INTO products (id,name,price,active) VALUES (1,'Desk',120,1),(2,'Lamp',35,1),(3,'Chair',85,0)");
$pdo->exec("UPDATE products SET active = 1 WHERE id = 1");
$info = $pdo->errorInfo();
echo $info[0] ?? "unknown";

Example 2: A Second Scenario


This self-contained SQLite example uses in-memory demo data; it requires the pdo_sqlite driver.

<?php
$pdo = new PDO('sqlite::memory:', null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$pdo->exec('CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT NOT NULL, price INTEGER NOT NULL, active INTEGER)');
$pdo->exec("INSERT INTO products (id,name,price,active) VALUES (1,'Desk',120,1),(2,'Lamp',35,1),(3,'Chair',85,0)");
$pdo->exec("UPDATE products SET active = 1 WHERE id = 2");
$info = $pdo->errorInfo();
echo $info[0] ?? "unknown";

Example 3: Practical Checks


This self-contained SQLite example uses in-memory demo data; it requires the pdo_sqlite driver.

<?php
$pdo = new PDO('sqlite::memory:', null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$pdo->exec('CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT NOT NULL, price INTEGER NOT NULL, active INTEGER)');
$pdo->exec("INSERT INTO products (id,name,price,active) VALUES (1,'Desk',120,1),(2,'Lamp',35,1),(3,'Chair',85,0)");
$pdo->exec("UPDATE products SET active = 1 WHERE id = 3");
$info = $pdo->errorInfo();
echo $info[0] ?? "unknown";

Important Behavior and Edge Cases


When using exceptions, inspect caught PDOException rather than exposing database diagnostics to users.

For production use, also account for error handling, data validation, resource limits and environment-specific driver behavior.

Comparison with Related Approaches


ApproachImportant distinction
PDO::errorInfoReturns SQLSTATE, driver-specific error code and driver-specific error message for the last connection operation.
Related API / patternerrorCode supplies only the SQLSTATE code.

Common Beginner Mistakes


MistakeCorrect approach
Assuming the happy path covers edge casesWhen using exceptions, inspect caught PDOException rather than exposing database diagnostics to users.
Using a related API interchangeablyerrorCode supplies only the SQLSTATE code.

Key Takeaways


  • Returns SQLSTATE, driver-specific error code and driver-specific error message for the last connection operation.
  • When using exceptions, inspect caught PDOException rather than exposing database diagnostics to users.
  • errorCode supplies only the SQLSTATE code.

Choose this API or pattern when its exact semantics match your task, and verify the result against the database driver and runtime used by your application.

Share this resourceSend this page to someone who may find it useful.