Home > Article > Backend Development > ## How to Inspect the Actual Query Executed by PDO in PHP?
How to Inspect the Parametrized Query in PHP with PDO
When utilizing PDO for database access in PHP, employing parametrized queries enhances security by shielding against SQL injection. However, it can be challenging to verify the actual query executed by the database, as the query with tokens and parameters is transmitted separately.
Direct Retrieval Not Possible
As Ben James explains, there is no direct method to obtain the complete SQL query on the PHP side. This is because the database receives the query with tokens and parameters independently, and the actual query is assembled within the database. Emulating the replacement process on the PHP side is also infeasible due to variations in token handling and binding techniques.
Logging as a Workaround
To inspect the actual query executed on the database, a workaround involves logging all SQL queries. In MySQL, this can be achieved by modifying the my.cnf (or my.ini in Windows environments) and adding a line like:
log=[REPLACE_BY_PATH]/[REPLACE_BY_FILE_NAME]
However, this should only be used for debugging purposes and not in production environments to avoid performance degradation.
The above is the detailed content of ## How to Inspect the Actual Query Executed by PDO in PHP?. For more information, please follow other related articles on the PHP Chinese website!