在带有PDO的PHP中,如何检查最终的SQL参数化查询? [英] In PHP with PDO, how to check the final SQL parametrized query?

查看:164
本文介绍了在带有PDO的PHP中,如何检查最终的SQL参数化查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP中,当使用带有参数化查询的PDO访问MySQL数据库时,如何检查最终查询(在替换了所有令牌之后)?

In PHP, when accessing MySQL database with PDO with parametrized query, how can you check the final query (after having replaced all tokens)?

有没有办法检查数据库真正执行了什么?

Is there a way to check what gets really executed by the database?

推荐答案

所以我想我将最终回答我自己的问题,以便为记录提供一个完整的解决方案.但是必须感谢本·詹姆斯和凯拉什·巴杜,他们为此提供了线索.

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.

简短答案
本·詹姆斯(Ben James)提到:.
PHP端不存在完整的SQL查询,因为带有令牌的查询和参数是分别发送到数据库的. 仅在数据库方面,完整查询存在.

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.

即使试图在PHP端创建一个用于替换令牌的函数也不能保证替换过程与SQL相同(如令牌类型,bindValue与bindParam之类的棘手东西)...

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, ...)

解决方法
这是我详细介绍Kailash Badu的答案的地方. 通过记录所有SQL查询,我们可以看到服务器上实际运行的内容. 使用mySQL,可以通过更新my.cnf(或Wamp服务器中的my.ini),并添加如下一行来完成此操作:

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]

请不要在生产环境中运行它!

这篇关于在带有PDO的PHP中,如何检查最终的SQL参数化查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆