Get query back from PDO prepared statement [duplicate]

Is there a way to retrieve the query that was used to generate a PDO Prepared statement object?


Solution 1:

Try $statement->queryString.

Solution 2:

The simplest way to achieve what you want is:

$statement->debugDumpParams();

Just make sure you add it after executing the statement.

Solution 3:

If you aren't opposed to extending the default \PDO and \PDOStatement object, you might consider looking at:

github.com/noahheck/E_PDOStatement

This extension to PDO allows you to see a full query statement as an example of what might be executed at the database level. It uses regex to interpolate the bound parameters of your PDO statement.

By extending the default \PDOStatement definition, E_PDOStatement is able to offer this enhancement to the default functionality without requiring modification to your normal work flow.

Disclaimer: I created this extension.

I just hope it's helpful to someone else.