【问题标题】:Why PDO debugDumpParams truncate query为什么 PDO debugDumpParams 会截断查询
【发布时间】:2015-06-07 05:49:38
【问题描述】:

我发现了同样的问题here,但没有得到解答,我在这里提供了更简单的示例,然后再次尝试提问...

代码:

<?php
$dbh = new PDO('mysql:dbname=test;host=127.0.0.1', 'root');
$sth = $dbh->prepare("
    SELECT '
        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, and the position in the query (if this is supported by the PDO driver, otherwise, it will be -1).
        This is a debug function, which dump directly the data on the normal output.
        Tip:
        As with anything that outputs its result directly to the browser, the output-control functions can be used to capture the output of this function, and save it in a string (for example).
        This will only dumps the parameters in the statement at the moment of the dump. Extra parameters are not stored in the statement, and not displayed.
    '
");
$sth->execute();
$sth->debugDumpParams();

结果:

SQL: [835] 
    SELECT '
        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, and the position in the query (if this is supported by the PDO driver, otherwise, it will be -1).
        This is a debug function, which dump directly the data on the normal output.
        Tip:
        As with anythi
Params:  0

为什么会发生,如何解决?
提前致谢!

【问题讨论】:

  • 我对源代码进行了一些挖掘,虽然我无法找到 exact 答案,但我能够确认它确实将所有查询减少到 500 个字符。从源代码来看,没有办法解决它,因为该方法不接受任何参数,它只是使用您无法用外部参数“控制”的内部方法写入输出流。您唯一的选择是使用 MySQL 并打开 GENERAL_LOG 并以这种方式捕获发送到 MySQL 的实际数据(这是我通常做的,我从未使用 PDO 进行调试)。
  • 已经创建了错误,bugs.php.net/bug.php?id=69356希望大家修复它。
  • 但是如果你仔细想想,你为什么需要查询呢?您已经拥有它,因为您是将它传递给 prepare 的人。您的原始问题在变量中也有查询。 debugDumpParams 唯一有趣的输出是参数,而不是实际的查询。
  • 编写新代码时是个不错的选择。但是当我已经编写了项目时,我想查看页面上的所有查询(已经写好的)。这不是解决方案。
  • 查询可能很大。你甚至可以将一些 MB 的二进制数据注入其中——相信我——这会使浏览器停止响应。日志记录设施在某处设置限制是正常的。

标签: php mysql sql pdo pdostatement


【解决方案1】:

我认为debugDumpParams 总体上是一个错误。事实上,它仅在标准输出中吐出数据!

所以无论如何我都不会使用它,并且出于日志记录的目的,要么为 mysql 启用一个通用日志,要么为具有日志记录功能的 PDO 创建一个包装器(此解决方案更便携)。

【讨论】:

    【解决方案2】:

    Why it occurs? - 我没有找到任何关于它的信息。
    How fix it? - 我没有找到任何线索如何使用本机 PDO 修复它。

    这个问题的主要目的是如何查看特定php脚本的所有查询
    我发现只有一种方法可以实现这一点 - 是 to enable general log for mysql

    【讨论】:

      猜你喜欢
      • 2019-10-17
      • 1970-01-01
      • 2021-07-19
      • 2013-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多