调试PDO查询,怎么做?

调试PDO查询,怎么做?

问题描述:

I'm having trouble with some of the more complicated queries sent through PDO using prepared statements. While errorInfo() is useful in showing syntax errors, it doesn't really show logic errors and what is actually part of the query.

Is there a way to echo out the query that PDO executed last? As in, the actual query sent to database? The class should store it somewhere right, because it does send it to database somewhere? Is there a method or some way of catching it?

Currently my debugging system echoes out only the query with ? in place of actual parameters, which is less useful if the error is in logic.

Thanks!

我在使用预处理语句通过PDO发送的一些更复杂的查询时遇到问题。 虽然errorInfo()在显示语法错误时非常有用,但它并不真正显示逻辑错误以及实际上是查询的一部分。 p>

是否有办法回显PDO的查询 最后执行? 如在,实际查询发送到数据库? 该类应该将它存储在正确的位置,因为它确实将它发送到某个地方的数据库? 是否有方法或某种方法来捕获它? p>

目前我的调试系统只回显查询? 代替实际参数,如果错误在逻辑中则不太有用。 p>

谢谢! p> div>

Did you try PDOStatement::debugDumpParams? It gives you detailed information about parameters.

From the manual:

Dumps the informations contained by a prepared statement directly on the output. It will provide the SQL query in use, the number of parameters used (Params), the list of parameters, with their name, type (paramtype) as an integer, their key name or position, the value, and the position in the query (if this is supported by the PDO driver, otherwise, it will be -1).

Consider trying mysql-proxy: http://lamp-dev.com/wp-content/uploads/mysql-proxy.zip

It doesn't matter what extension you use in php , it is just in the middle between php and mysql acting as proxy and shows you all queries "as is". You just need to change DSN in start.bat. Note that this version is windows based.

You'll need to change your connection in php then to connect it to mysql-proxy

Forgot to say that this makes sense only if you use mysql, you did not specify your RDBMS, PDO may work with different drivers