Posts tagged ‘php’

PDOStatement returns false but there’s no error

I ran into this a little while ago but didn’t blog it, then someone I know in ##phpbartalk (on freenode IRC) ran into this again today. Cost him hours, apparently.

PDOStatement::execute was returning false, but a calls to PDOStatement::errorCode and PDOStatement::errorInfo were returning no useful information. The query in question was an insert, and no row was being inserted into the database. Clearly the query was failing, but there was no indication why.

I remembered dimly running into this myself, and looked at the php manual entry for PDO (specifically, this error handling page). Apparently the default error handling mode for PDO is PDO::ERRMODE_SILENT, which is less than helpful in this case. In the user comments, this was unearthed:

$dbh = new PDO( /* blah blah blah connection string */ );

Turns out that it was a malformed query (dropped a trailing parenthesis) that gave a SQL error when run manually. However, for some reason PDO was issuing a warning instead of an error, and because of the ATTR_ERRMODE, it wasn’t even issuing that. Less than helpful, but I’m sure there’s a reason for the default behavior. What it is.. well, someone can enlighten me on that one. I’d sure like to know if a query were generating an error…