PDO prepare() fails or returns false and shows no SQL error
prepare() returns false and no error is shownShort answer
PDO reports SQL errors the way the PDO::ATTR_ERRMODE setting says. In silent mode prepare() returns false and says nothing. Set the mode to exceptions so the database message comes out, or read errorInfo(). With emulated prepares, a bad statement may not fail in prepare() at all, only later in execute().
Fix it fast
1Find out which cause you have
Check whether the connection is in exception mode. false means silent or warning mode.
var_dump($pdo->getAttribute(PDO::ATTR_ERRMODE) === PDO::ERRMODE_EXCEPTION);2Match what you see, then jump to its fix
- 1
prepare() returns false and no message is shown anywhere.The error mode is silent, so nothing is reportedFix - 2
Existing code expects false return values and cannot be changed yet.You must keep silent mode and read the error yourselfFix - 3
prepare() succeeds, and the SQL error only appears at execute().Emulated prepares do not check the statementFix
- 1
What does the error look like?
prepare() returns false and no error is shownFatal error: Uncaught PDOException: SQLSTATE[42S02]: Base table or view not found
Cause 1 of 3
The error mode is silent, so nothing is reported
1Check
Before PHP 8.0 the default mode was silent. If your code or framework sets PDO::ERRMODE_SILENT, errors only show through errorCode() and errorInfo().
grep -rn "ERRMODE" src config2Fix
Set exceptions in the constructor options, and catch PDOException where you handle database work.
$pdo = new PDO($dsn, $user, $pass, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);
try {
$stmt = $pdo->prepare('SELECT name FROM fruit WHERE calories < :calories');
$stmt->execute(['calories' => 150]);
} catch (PDOException $e) {
error_log($e->getMessage());
}3Verify
A wrong table or column now throws with an SQLSTATE and the database's own message.
php test.phpConfirmed by php.net
Cause 2 of 3
You must keep silent mode and read the error yourself
1Check
In silent mode PDO sets error codes only. Errors from the database object are on the PDO object, errors from a statement are on the PDOStatement.
grep -rn "->prepare(" src | head2Fix
Test the return value and print errorInfo(), which holds the SQLSTATE, the driver code and the driver message.
$stmt = $pdo->prepare($sql);
if ($stmt === false) {
[$sqlstate, $driverCode, $driverMessage] = $pdo->errorInfo();
error_log("$sqlstate $driverCode $driverMessage");
}3Verify
The log line shows the database's message for the failed statement.
tail -n 5 /var/log/php_errors.logConfirmed by php.net
Cause 3 of 3
Emulated prepares do not check the statement
1Check
The manual says emulated prepared statements do not talk to the database server, so prepare() does not check the statement.
var_dump($pdo->getAttribute(PDO::ATTR_EMULATE_PREPARES));2Fix
Wrap both prepare() and execute() in the same try block, since errors from either one are thrown in exception mode. To have the server check the statement at prepare() time, turn emulation off.
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);3Verify
A bad statement now fails at prepare() with the server's error.
php test.phpConfirmed by php.net
How do I stop it coming back?
- Set PDO::ERRMODE_EXCEPTION in one central place where the connection is created.
- Catch PDOException at the boundary of your database code and log getMessage().
- Never show the raw message to visitors; log it.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- ShopGoAn e-commerce aggregator with a Python scraping engine behind its catalog, past 10K Google Play downloads.
Related fixes
Back end
CodeIgniter 4: try/catch does not catch a failed insert in the model
DatabaseException
CodeIgniter throws a DatabaseException on a failed query only when DBDebug is true, and inside a transaction it does not throw unless you ask.
3 causes, each with a fixBack end
CodeIgniter 4 where() builds a different condition than you meant
A SQL syntax error that appears only when a value contains a quote character
The query builder joins every where() with AND and every orWhere() with OR, with no brackets.
3 causes, each with a fixBack end
Call to undefined function mysql_pconnect() after a PHP upgrade
Call to undefined function mysql_connect()
The old mysql_* functions belonged to the ext/mysql extension, which PHP removed in version 7.0.
2 causes, each with a fixBack end
CodeIgniter cannot connect to the MySQL database server
Unable to connect to your database server using the provided settings.
CodeIgniter only reports that its connection failed.
4 causes, each with a fixBack end
Laravel insert fails with 'Field user_id doesn't have a default value'
The insert works when the column is removed or made nullable
Error 1364 means Laravel sent an INSERT without a value for a NOT NULL column that has no default, and MySQL strict mode refuses it.
3 causes, each with a fixBack end
Laravel migration cannot drop a column that has a foreign key
Error on rename of '%s' to '%s' (errno: %d - %s)
MySQL refuses to drop a column while a foreign key constraint still uses it.
2 causes, each with a fix