tags:

views:

23

answers:

2
+1  Q: 

PDO error message?

Here is a snippet of my code:

$qry = '
    INSERT INTO non-existant-table (id, score) 
    SELECT id, 40 
    FROM another-non-existant-table
    WHERE description LIKE "%:search_string%"
    AND available = "yes"
    ON DUPLICATE KEY UPDATE score = score + 40
';
$sth = $this->pdo->prepare($qry);
$sth->execute($data);

print_r($this->pdo->errorInfo());

This should give me an error because the tables don't even exist. All I get however is this:

Array ( [0] => 00000 )

How can I get a better description of the error so I can debug the issue?

+1  A: 

Try this instead:

print_r($sth->errorInfo());

EDIT:

Add this before your prepare:

$this->pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );

This will change the PDO error reporting type and cause it to emit a warning whenever there is a PDO error. It should help you track it down, although your errorInfo should have bet set.

Alan Geleynse
Thanks, that gives me the exact same thing, `Array ( [0] => 00000 )`.
John Isaacks
I added another thing you can try, but if errorInfo is not getting set correctly there may be something else going on.
Alan Geleynse
This appears to be working.
John Isaacks
A: 

From the manual:

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

The prepare statement likely caused an error because the db would be unable to prepare the statement. Try testing for an error immediately after you prepare your query and before you execute it.

$qry = '
    INSERT INTO non-existant-table (id, score) 
    SELECT id, 40 
    FROM another-non-existant-table
    WHERE description LIKE "%:search_string%"
    AND available = "yes"
    ON DUPLICATE KEY UPDATE score = score + 40
';
$sth = $this->pdo->prepare($qry);
print_r($this->pdo->errorInfo());
thetaiko