Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

PDO prepare() fails or returns false and shows no SQL error

Errorprepare() returns false and no error is shown

Short 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().

  • Applies to PHP 8.3, PHP 8.4, PDO MySQL, MySQL 8
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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);
  2. 2Match what you see, then jump to its fix

    1. 1prepare() returns false and no message is shown anywhere.The error mode is silent, so nothing is reportedFix
    2. 2Existing code expects false return values and cannot be changed yet.You must keep silent mode and read the error yourselfFix
    3. 3prepare() succeeds, and the SQL error only appears at execute().Emulated prepares do not check the statementFix

What does the error look like?

  • prepare() returns false and no error is shown
  • Fatal 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 config

2Fix

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.php

Confirmed by php.net

Did this fix it?

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 | head

2Fix

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.log

Confirmed by php.net

Did this fix it?

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.php

Confirmed by php.net

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.