PHP Error Handling in DB
In this page:
Catching Database Exceptions
Database operations can fail for many reasons — a lost connection, a constraint violation, a syntax error in a query — and how your code reacts to those failures determines whether a user sees a clear message or a broken page.
Example: Catching Database Exceptions
<?php
try {
$pdo = new PDO('sqlite::memory:');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec("SELECT * FROM missing_table");
} catch (PDOException $e) {
echo "A clear error was caught instead of a broken page";
}
?>
Login to try C/C++/Java/PHP code in the editor
Reading Error Codes
Setting PDO::ATTR_ERRMODE to PDO::ERRMODE_EXCEPTION (or checking mysqli_error() after each mysqli call) ensures database failures surface immediately instead of being silently ignored and causing confusing bugs downstream.
Example: Reading Error Codes
<?php
$pdo = new PDO('sqlite::memory:');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Errors will now surface immediately as exceptions";
?>
Login to try C/C++/Java/PHP code in the editor
Custom Error Loggers
Wrapping database calls in a try/catch block lets you catch a PDOException specifically, log the technical details for developers, and show the end user a friendly, non-technical error message instead.
Example: Custom Error Loggers
<?php
try {
$pdo = new PDO('sqlite::memory:');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec("SELECT * FROM missing_table");
} catch (PDOException $e) {
error_log($e->getMessage());
echo "Something went wrong. Please try again.";
}
?>
Login to try C/C++/Java/PHP code in the editor
MySQLi Connection Errors
Never display raw database error messages directly to end users — they can leak details about your schema or query structure that an attacker could use, and are generally meaningless to a non-technical visitor anyway.
Example: MySQLi Connection Errors
<?php
try {
$pdo = new PDO('sqlite::memory:');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec("SELECT * FROM missing_table");
} catch (PDOException $e) {
// Never echo $e->getMessage() directly to the visitor -- it can leak schema details
echo "An error occurred.";
}
?>
Login to try C/C++/Java/PHP code in the editor
Suppressing Sensitive Details
Logging the full exception (including the query and parameters, but never raw passwords) to a server-side log file gives you the detail you need to debug an issue without exposing anything sensitive to the public.
Example: Suppressing Sensitive Details
<?php
try {
$pdo = new PDO('sqlite::memory:');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec("SELECT * FROM missing_table");
} catch (PDOException $e) {
error_log("DB error: " . $e->getMessage());
echo "Logged for developers, hidden from the visitor.";
}
?>
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 21 topics to unlock
0/21 topics done
Complete these topics first:
- PHP MySQL Introduction
- PHP MySQLi Connection
- PHP PDO Introduction
- PHP CRUD Operations
- PHP Prepared Statements
- PHP Stored Procedures
- PHP Transactions
- PHP Error Handling in DB
- PHP MySQL Connect
- PHP MySQL Create DB
- PHP MySQL Create Table
- PHP MySQL Insert Data
- PHP MySQL Get Last ID
- PHP MySQL Insert Multiple
- PHP MySQL Prepared Statements
- PHP MySQL Select Data
- PHP MySQL Where
- PHP MySQL Order By
- PHP MySQL Delete Data
- PHP MySQL Update Data
- PHP MySQL Limit Data