In PHP with PDO, how to check the final SQL parametrized query? [duplicate]
Solution 1:
So I think I'll finally answer my own question in order to have a full solution for the record. But have to thank Ben James and Kailash Badu which provided the clues for this.
Short Answer
As mentioned by Ben James: NO.
The full SQL query does not exist on the PHP side, because the query-with-tokens and the parameters are sent separately to the database.
Only on the database side the full query exists.
Even trying to create a function to replace tokens on the PHP side would not guarantee the replacement process is the same as the SQL one (tricky stuff like token-type, bindValue vs bindParam, ...)
Workaround
This is where I elaborate on Kailash Badu's answer.
By logging all SQL queries, we can see what is really run on the server.
With mySQL, this can be done by updating the my.cnf (or my.ini in my case with Wamp server), and adding a line like:
log=[REPLACE_BY_PATH]/[REPLACE_BY_FILE_NAME]
Just do not run this in production!!!
Solution 2:
You might be able to use PDOStatement->debugDumpParams
. See the PHP documentation .
Solution 3:
Using prepared statements with parametrised values is not simply another way to dynamically create a string of SQL. You create a prepared statement at the database, and then send the parameter values alone.
So what is probably sent to the database will be a PREPARE ...
, then SET ...
and finally EXECUTE ...
.
You won't be able to get some SQL string like SELECT * FROM ...
, even if it would produce equivalent results, because no such query was ever actually sent to the database.
Solution 4:
I check Query Log to see the exact query that was executed as prepared statement.