Search code examples
phppdoprepare

PDO errorCode doesn't work with prepare


$sql = "dSELECT * FROM users";
$dbQuery = $this->dbal->query($sql);
$dbError = $this->dbal->errorInfo();

$dbError gets the syntax error information.

If the same is used for prepared statement it doesn't return any error after prepare.

This code is from php.net

<?php
/* Provoke an error -- bogus SQL syntax */
$stmt = $dbh->prepare('bogus sql');
if (!$stmt) {
    echo "\nPDO::errorInfo():\n";
    print_r($dbh->errorInfo());
}

ErrorInfo doesn't work in this way. I use

$dbQuery = $this->dbal->prequery($sql);
$dbError = $dbQuery->errorInfo(); // $this->dbal->errorInfo(); doesn't work, too.

Solution

  • The manual says:

    If the database server successfully prepares the statement, PDO::prepare() returns a PDOStatement object. If the database server cannot successfully prepare the statement, PDO::prepare() returns FALSE or emits PDOException (depending on error handling).

    That means that you don't have an object when it fails: you have a boolean FALSE.

    Try changing the PDO error handling to PDO::ERRMODE_EXCEPTION.