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:
[gist id=”08672aac2c6baf224130″ file=”1-debugging-pdo-prepared-statements-pdo.php” highlight=”2,6,10,43,95-100″]
This results in the following output:
[gist id=”08672aac2c6baf224130″ file=”2-debugging-pdo-prepared-statements-output.sql” highlight=”2,6,10,43,95-100″]
Hopefully, this will help you get a somewhat better idea of what’s going on :)
Comments
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.
Comments are closed.