查看和调试准备好的PDO查询,而无需查看MySQL日志 [英] View and debug prepared PDO query without looking at MySQL logs
本文介绍了查看和调试准备好的PDO查询,而无需查看MySQL日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想看看PDO正在准备什么而无需查看MySQL日志.基本上,它是在执行查询之前就建立的最终查询.
I want to see what PDO is preparing without looking into the MySQL logs. Basically the final query it has built right before it executes the query.
有没有办法做到这一点?
Is there a way to do this?
推荐答案
没有内置方法. bigwebguy 创建了一个函数来实现
There is no built-in way to do it. bigwebguy created a function to do it in one of his answers:
/**
* Replaces any parameter placeholders in a query with the value of that
* parameter. Useful for debugging. Assumes anonymous parameters from
* $params are are in the same order as specified in $query
*
* @param string $query The sql query with parameter placeholders
* @param array $params The array of substitution parameters
* @return string The interpolated query
*/
public static function interpolateQuery($query, $params) {
$keys = array();
# build a regular expression for each parameter
foreach ($params as $key => $value) {
if (is_string($key)) {
$keys[] = '/:'.$key.'/';
} else {
$keys[] = '/[?]/';
}
}
$query = preg_replace($keys, $params, $query, 1, $count);
#trigger_error('replaced '.$count.' keys');
return $query;
}
这篇关于查看和调试准备好的PDO查询,而无需查看MySQL日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文