I'm making the change from mysql_*
calls to using PDO calls and am unable to figure out how to debug my actual SQL when developing new code.
我正在进行从mysql_ *调用到使用PDO调用的更改,并且在开发新代码时无法弄清楚如何调试我的实际SQL。
Under mysql_*
calls I could write some SQL in a PHP function and could echo
the actual SQL to see what the processor was working with.
在mysql_ *调用下,我可以在PHP函数中编写一些SQL,并且可以回显实际的SQL以查看处理器正在使用的内容。
I haven't been able to find such a beastie in the PDO library. debugDumpParams
looks like it should, but it doesn't spit back the bound statement.
我无法在PDO库中找到这样的野兽。 debugDumpParams看起来应该如此,但它不会吐出绑定语句。
Examples of problems I have encountered:
我遇到的问题的例子:
-
In my very first attempt I was binding a string and was including the quotes in the SQL statement, despite binding with a data_type of string - it was only by fluke I tried removing the quotes from the statement; debugging would have let me see there was a repeated quote in there.
在我的第一次尝试中,我绑定了一个字符串并在SQL语句中包含了引号,尽管绑定了一个data_type字符串 - 它只是通过fluke我尝试从语句中删除引号;调试会让我看到那里有一个重复的引用。
-
I copied some code from one project to another and accidentally forgot to correct the database name. Naturally the SQL failed because the tables didn't exist in the other DB. But the program just returned the correct false result. Nowhere in the PHP logs or the MySQL logs or anywhere did I get a hint that the table didn't exist where I was looking for it.
我将一些代码从一个项目复制到另一个项目,并意外忘记更正数据库名称。当然,SQL失败了,因为其他数据库中不存在这些表。但该程序刚刚返回了正确的错误结果。在PHP日志或MySQL日志中没有任何地方或任何地方,我得到一个暗示,表我不在寻找它的地方。
So, how are other people doing debugging for PDO SQL calls? What am I missing? :)
那么,其他人如何为PDO SQL调用进行调试呢?我错过了什么? :)
1 个解决方案
#1
4
For the first problem, checking the query is difficult indeed. There isn't much you can do about this except for logging the queries on your database server. The reason is that prepared queries are not (always) simply concatenating data into the query. The whole point is that the data is sent separately from the query. You can access the query string though via $yourStatement->queryString
. It will only show the query with the parameters.
对于第一个问题,检查查询确实很困难。除了在数据库服务器上记录查询之外,您无能为力。原因是准备好的查询不是(总是)简单地将数据连接到查询中。重点是数据是从查询中单独发送的。您可以通过$ yourStatement-> queryString访问查询字符串。它只会显示带参数的查询。
For your second problem, by default PDO doesn't throw an exception when an error occurs. It's up to you to check for them. You can change this though. http://php.net/manual/en/pdo.error-handling.php
对于第二个问题,默认情况下,PDO在发生错误时不会抛出异常。由你来检查它们。你可以改变这个。 http://php.net/manual/en/pdo.error-handling.php
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Then, you can catch problems by a try/catch block around your query execution.
然后,您可以通过查询执行的try / catch块来捕获问题。
#1
4
For the first problem, checking the query is difficult indeed. There isn't much you can do about this except for logging the queries on your database server. The reason is that prepared queries are not (always) simply concatenating data into the query. The whole point is that the data is sent separately from the query. You can access the query string though via $yourStatement->queryString
. It will only show the query with the parameters.
对于第一个问题,检查查询确实很困难。除了在数据库服务器上记录查询之外,您无能为力。原因是准备好的查询不是(总是)简单地将数据连接到查询中。重点是数据是从查询中单独发送的。您可以通过$ yourStatement-> queryString访问查询字符串。它只会显示带参数的查询。
For your second problem, by default PDO doesn't throw an exception when an error occurs. It's up to you to check for them. You can change this though. http://php.net/manual/en/pdo.error-handling.php
对于第二个问题,默认情况下,PDO在发生错误时不会抛出异常。由你来检查它们。你可以改变这个。 http://php.net/manual/en/pdo.error-handling.php
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Then, you can catch problems by a try/catch block around your query execution.
然后,您可以通过查询执行的try / catch块来捕获问题。