61

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();
}

It is giving out only:

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

setAttribute doesn't help anything.

It's 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 information?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345

4 Answers4

66

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
  • 442,112
  • 142
  • 972
  • 1,088
  • man page example doesn't execute either: http://ru2.php.net/manual/en/pdo.errorinfo.php and with execute still no luck – Your Common Sense Sep 16 '10 at 12:16
  • 2
    @Col what if you set `$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );`? – Pekka Sep 16 '10 at 12:18
  • @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 Sep 16 '10 at 12:23
  • this one worked. Add it to the answer please, so I can accept it. though it still puzzling me, why errorInfo() doesn't work – Your Common Sense Sep 16 '10 at 12:23
  • @Col I added it. PDO is extremely secretive about database errors, but I would have expected `errorInfo()` to work, too – Pekka Sep 16 '10 at 12:25
  • 1
    Emulated prepare statements seems to be turned on by default for Mysql PDO.... for some reason. You have to explicitly turn emulation off. – Chris Baker Apr 10 '13 at 17:18
5

I too was trying to get the information from errorInfo() at the database handle level, but I ended up getting the information from the statement level with PDOStatement::errorInfo()

Per PHP web site:

PDO::errorInfo() only retrieves error information for operations performed directly on the database handle. If you create a PDOStatement object through PDO::prepare() or PDO::query() and invoke an error on the statement handle, PDO::errorInfo() will not reflect the error from the statement handle. You must call PDOStatement::errorInfo() to return the error information for an operation performed on a particular statement handle.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
BitsAndBytes
  • 815
  • 1
  • 9
  • 13
4

This will print error code as well its corresponding detailed message.

Advice: this is just a demonstration. Just use for debugging purpose. Don't enable to show error messages to the public in a release version.

try{
connection=$this->get_connection();//here i brought my connection string
connection->setAttribute(PDO::ATTR_EMULATE_PREPARES,false);
connection->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
/**
Do your works here..
//$statement=$connection->prepare($sql);
//if you are using errorInfo use after prepare statement before execute.here in this method i am not using it.
//print_r($statement->errorInfo());
**/

$statement->execute();
}
catch(PDOException $e) {
              //this will echo error code with detail
              //example: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'nasme' in 'field list'
              echo $e->getMessage();
            }
//$statement=null;
halfer
  • 19,824
  • 17
  • 99
  • 186
aimme
  • 6,385
  • 7
  • 48
  • 65
  • i don't understand why my answer was edited so badly n minus voted(-) as we know it was a demonstration of how to do something. its the only way to accomplish it while debugging the code. it shows detailed error message with error code.now i don't seem this answer is anything :/ why not delete it .. – aimme Aug 18 '15 at 20:26
  • Ah, you've restored the original code - fair enough. If it gets modified in a way that changes your intent again, please raise a moderator flag rather than engaging in an edit war - thanks. I'll fix the all-caps - we have a 'no shouting' rule here `:-)` – halfer Aug 18 '15 at 20:52
  • 3
    @YourCommonSense: please leave this answer alone. It already has a moderator report. If you have some feedback for the solution, feel free to expand upon the link you have provided in a comment. – halfer Aug 19 '15 at 06:55
3

You need to first execute 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.

Marty
  • 39,033
  • 19
  • 93
  • 162
shamittomar
  • 46,210
  • 12
  • 74
  • 78