How to print SQL in PDO

Ok, I just created a prepared statement with PDO. bind the values to the parameters in the SQL that was used to create the statment. finally executed the SQL. The query didn’t work. Now I want to know the exact SQL query that was executed by the database?

How do I print that out?

I don’t know of a way to get it out of PDO, but you can turn on query logging on the database server to see what it received.

http://dev.mysql.com/doc/refman/5.0/en/query-log.html

Yo could also take a look at PDOStatement->debugDumpParams.

Dumps the informations contained by a prepared statement directly on the output. It will provide the SQL query in use, the number of parameters used (Params), the list of parameters, with their name, type (paramtype) as an integer, their key name or position, the value, and the position in the query (if this is supported by the PDO driver, otherwise, it will be -1).

Most likely the problem is before the binding occurs. Do you know the problem occurs due to the binding or are you assuming it does?

Agree with Dan, go look in the logs - that’s pretty efficient.

If you need your app to handle it, then inspect $stmt->errorInfo() or $smt->errorCode().

I’m currently struggling with trying to get these kind of checks eradicated.


if( $stmt->errorCode() != '00000' ){
// smthgs knackered
// chuck exception
}

Off Topic:

SilverBulletUK is becoming americanized, dude. :slight_smile:

You can also get the parameterized string by building it up as a var.


$PDO->prepare( $sql ) ;

// later

if (error detected)
echo $sql ;

Where is $sql is the string with the place holders, doesn’t reveal the values though.