views:

65

answers:

2

I can't seem to get any error message from PDO

#$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
try {
  $sth = $dbh->prepare('@$%T$!!!');
  print_r($sth);
  print_r($dbh->errorInfo());
} catch (PDOException $e) {
    echo $e->getMessage();
}

giving out only

PDOStatement Object
(
    [queryString] => @$%T$!!!
)
Array
(
    [0] => 00000
    [1] => 
    [2] => 
)

setAttribute doesn't help anything.

Its PHP 5.3.3 Apache 2.0 Handler
PDO Driver for MySQL enabled
Client API version mysqlnd 5.0.7-dev - 091210 - $Revision: 300533 $

What can I do to get error info?

+2  A: 

You need to first ececute the query and then check for errors: So do it like this:

 $sth->execute();

and then check for errors. Then you will get errors, if any.

shamittomar
+4  A: 

setAttribute will cause PDO to throw up errors or exceptions - the latest when you execute the query.

For emulated prepared statements, there is no check in prepare():

Emulated prepared statements does not communicate with the database server so PDO::prepare() does not check the statement.

But there will be one in execute() when the query gets sent to the server.

However, the mySQL driver supports native prepared statements since mySQL 4.1 anyway, so this shouldn't apply. Using

$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

must cause an exception for the query you use.

Pekka
man page example doesn't execute either: http://ru2.php.net/manual/en/pdo.errorinfo.php and with execute still no luck
Col. Shrapnel
@Col what if you set `$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );`?
Pekka
@Col If you set `ERRMODE_EXCEPTION` and there are no exceptions neither from `prepare()` nor `exec()` then something else is wrong - in that case, I have no idea what
Pekka
this one worked. Add it to the answer please, so I can accept it. though it still puzzling me, why errorInfo() doesn't work
Col. Shrapnel
@Col I added it. PDO is extremely secretive about database errors, but I would have expected `errorInfo()` to work, too
Pekka
Yes, that worked.
shamittomar