Debugging PDO Prepared Statements

Standard

Something that has always bugged me about using prepared statements, is that you can really only get the query sent to the database by catching it in the logs.

Today, a friend asking me if it was possible to get a prepared statement back from PDO with the values placeholders replaced, finally caught me in a moment where I could do something about it.

I wrote a thin PDO wrapper class that will [imperfectly, I’m sure] return the completed query.

It supports bound parameters, values and the array key->value methods of passing in values to prepared queries. You can see the code and examples below:

This results in the following output:

Hopefully, this will help you get a somewhat better idea of what’s going on :)

2 thoughts on “Debugging PDO Prepared Statements

  1. Marques Johansson

    I have a similar class in my projects. Unless I’m missing something, this implementation won’t work for ‘?’ parameters.

    • Davey Shafik

      You’re absolutely right. I don’t use them, and advise against it when possible; ‘?’ placeholders suck for readability and maintenance standpoints.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>