简体   繁体   English

db2_execute()之后,PHP DB2获取完整查询

[英]PHP DB2 Get Full Query after db2_execute()

We are using db2_prepare and db2_execute to prepare queries in a generic function. 我们正在使用db2_prepare和db2_execute在通用函数中准备查询。 I am trying to have a debug method to get the full prepared query after the '?' 我正在尝试一种调试方法来在“?” 之后获取完整的准备好的查询。 values have been replaced by the db2_execute function. 值已被db2_execute函数代替。

Is there an efficient way of doing this besides manually replacing each '?' 除了手动替换每个“'”以外,还有其他有效的方法吗? with the parameters I am passing in? 我传递的参数? ie is there a flag that can be set for db2_execute? 即是否可以为db2_execute设置一个标志?

Example: 例:

$params = array('xyz','123');
$query = "SELECT * FROM foo WHERE bar = ? AND baz = ?";
$sqlprepared = db2_prepare($CONNECTION, $query);
$sqlresults = db2_execute($sqlprepared,$params);

I would like the $sqlresults to contain the full prepared query: 我希望$ sqlresults包含完整的准备好的查询:

"SELECT * FROM foo WHERE bar = 'xyz' AND baz = '123'";

I have looked through the docs and do not see any obvious way to accomplish this, but I imagine there must be a way. 我浏览了文档,没有看到任何明显的方法来完成此操作,但我想一定有办法。

Thank you! 谢谢!

db2_execute() does not replace parameter markers with values. db2_execute()不会用值替换参数标记。 Parameters are sent to the server and bound to the prepared statement there. 参数被发送到服务器并绑定到服务器上的准备好的语句。

The CLI trace, which can be enabled on the client as explained here , will contain the actual parameter values. CLI跟踪,它可以在客户端上启用按此处的说明 ,将包含实际的参数值。 Keep in mind that the trace seriously affects application performance. 请记住,跟踪严重影响了应用程序性能。

I ended up writing a loop to replace the '?' 我最终写了一个循环来替换“?” parameters with a simple preg_replace after and outputting the query in my 'debug' array key: 参数,然后在我的“调试”数组键中输出查询后使用简单的preg_replace进行查询:

$debugquery = $query;
foreach($params as $param) {
  $debugquery = preg_replace('/\?/',"'".$param."'",$debugquery,1);
}
return $debugquery;

This handled what I needed to do (to print the finalized query for debugging purposes). 这处理了我需要做的事情(打印最终查询以用于调试目的)。 This should not be run in Production due to performance impacts but is useful to look at the actual query the server is trying to perform (if you are getting unexpected results). 由于性能影响,不应在生产中运行此命令,但对于查看服务器尝试执行的实际查询(如果得到意外结果)很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM