PDOException
PHP Programming Language
Severity: CriticalWhat Does This Error Mean?
A PDOException is an error thrown by PHP's PDO database library. PDO (PHP Data Objects) is the modern way to connect to databases in PHP. When something goes wrong — wrong password, bad SQL, lost connection — PDO throws a PDOException. If you do not catch it, PHP displays a fatal error and stops the script.
Affected Models
- PHP 5.1+
- PHP 7.x
- PHP 8.x
- All PHP versions with PDO extension
Common Causes
- Wrong database credentials — incorrect hostname, username, password, or database name
- The database server is not running or is unreachable from the web server
- A SQL syntax error in a query passed to PDO
- Trying to call a method on a PDO object that failed to connect (is null)
- The PDO extension for the required database driver (e.g. pdo_mysql) is not enabled
How to Fix It
-
Check your database credentials. Verify the hostname, port, database name, username, and password are all correct.
Even a single wrong character in the password causes a connection failure. Copy the credentials directly from your hosting panel.
-
Wrap your PDO connection in a try/catch block to handle exceptions gracefully instead of crashing.
try { $pdo = new PDO($dsn, $user, $pass); } catch (PDOException $e) { error_log($e->getMessage()); die('Database error.'); }
-
Set PDO error mode to exceptions in your connection: $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
This ensures all PDO errors throw exceptions that you can catch, rather than failing silently.
-
Check that the PDO MySQL driver is enabled. Run phpinfo() and search for 'PDO' on the page.
You should see 'pdo_mysql' in the list of enabled PDO drivers. If not, enable it in php.ini or contact your host.
-
Never display the full PDOException message to users. Log it privately with error_log() and show a generic 'Something went wrong' message.
The exception message often contains your database name, server path, or query — sensitive information you should never expose.
When to Call a Professional
If PDOExceptions are appearing on a live production site, fix them urgently. Exposed database error messages can reveal your database structure to attackers. A PHP developer can help you add proper error handling that logs errors privately and shows safe messages to users.
Frequently Asked Questions
What is the difference between PDO and MySQLi?
Both are modern PHP extensions for connecting to MySQL databases. PDO supports many database types — MySQL, PostgreSQL, SQLite, and more. MySQLi only works with MySQL. PDO is more portable if you ever need to switch databases. Both support prepared statements, which protect against SQL injection.
What is a DSN in PDO?
DSN stands for Data Source Name. It is a string that tells PDO how to connect to your database. For MySQL it looks like: 'mysql:host=localhost;dbname=mydb;charset=utf8mb4' You pass this as the first argument to new PDO().
Should I show PDOException errors on my live site?
Never. PDO error messages can include your database host, database name, table names, and SQL queries. This is a serious security risk. Log errors privately with error_log() and show a simple error message to the user.