dq23171 2012-01-13 14:30
浏览 67
已采纳

匹配SQL查询中的绑定参数

I use parameterized queries, like this:

SELECT * FROM foo WHERE bar = :p0 AND baz = :p1

My parameter names take the form:

^:[a-z]\d+$

I'm currently extending PDOStatement to provide a method for dumping a fully constructed query, because PDOStatement::queryString does not have the bind parameter values replaced.

What is the most accurate method for matching these parameters inside their query with a regular expression?

A not-so-accurate initial attempt:

$sql = "SELECT * FROM foo WHERE bar = 'bar:a0bar :u2 barbar :w4' AND baz = :q2 AND boz IN (:z6, :yy1, :q, :r22, :b7)";
$matches = array();
preg_match_all('/(:[a-z]\d+)\b/', $sql, $matches);
$params = $matches[1];

This fails because parameters within strings are matched, but I'm not sure it's feasible to avoid this.

Bear in mind that I know full well that no method will be 100% accurate, and this is just for dumping constructed queries to a log file to aid in debugging, so the resulting queries will not be sent to the database for execution.

  • 写回答

2条回答 默认 最新

  • dsgwdigu84950 2012-03-13 14:29
    关注

    I believe the solution I'm already currently using is probably the most practical. Anything beyond this would seem to add unnecessary complexity.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢