动态从 MySQL 字符串中提取 WHERE 组件
假设我有以下字符串(忘记它是一个MySQL语句):
SELECT * FROM users WHERE name = 'fred bloggs' AND age=21 AND address='Mountain View, CA 94043' LIMIT 1
我需要一种方法来提取WHERE子句中的字段名称和值,所以我有一个像这样的数组:
Array
(
[name] => fred bloggs
[age] => 21
[address] => Mountain View, CA 94043
)
记住这是一个动态的MySQL 字符串,因此我无法硬编码姓名、年龄或地址。
我可以预见的问题是:
- 查找字段名称,该函数必须知道所有有效的运算符,以便匹配每个字段名称(请参阅下面要使用的数组)
- 不保证空格(例如,age=21)
- 查找 ' 和 中的值' 但当 ' 位于字符串内时不会中断
- 查找不在 ' 和 ' 内的值,例如数字或其他字段名称(需要单独处理)
有谁知道如何使用正则表达式或字符串函数来执行此操作?
如果需要,以下是运算符:
$operators = array('+', '-', '*', '/', '^', '=', '<>', '!=', '<', '<=', '>', '>=', 'like', 'clike', 'slike', 'not', 'is', 'in', 'between', 'and', 'or');
Say I have the following string (forget that it is a MySQL statement):
SELECT * FROM users WHERE name = 'fred bloggs' AND age=21 AND address='Mountain View, CA 94043' LIMIT 1
I need a way to extract the field name and value in the WHERE clause so I have an array like:
Array
(
[name] => fred bloggs
[age] => 21
[address] => Mountain View, CA 94043
)
Remember this is a dynamic MySQL string so I can't hard-code name, age or address.
The problems I can foresee are:
- Finding the field names, the function would have to know all valid operators so to match each field name (see array to use below)
- Spaces are not guaranteed (e.g. age=21)
- Finding the values within ' and ' but not breaking when a ' is within the string
- Finding values not within ' and ' e.g. numbers or other field names (would need to be treated seperately)
Does anyone have any ideas how to do this either with regex or string functions?
Here are the operators if required:
$operators = array('+', '-', '*', '/', '^', '=', '<>', '!=', '<', '<=', '>', '>=', 'like', 'clike', 'slike', 'not', 'is', 'in', 'between', 'and', 'or');
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以尝试使用这种工具:
http://code.google.com/p/php-sql-parser/
你将得到一棵描述查询的树(一个数组),这将帮助你构建你想要的数组。
You can try with this kind of tool :
http://code.google.com/p/php-sql-parser/
You will get a tree (an array) describing the query, this will help you to build the array you want.
您应该为此创建一个表达式解析器,它无法通过 regex 实现。例如,您的结果数组将包含一个表达式树,其中包含您指定的所有节点类型(OR、AND、IN、BETWEEN 等)
网上有一个示例:此处
You should create an expresssion parser for that, it cannot be achieved with regex . E.g., your resulting array would contain an expression tree with all node types you specified (OR, AND, IN, BETWEEN, etc.)
There's an example on the web: here
可能有几种方法可以解决这个问题:
1)从查询中查找元数据。我知道您可以获得有关结果集的信息,但我不确定原始查询。它就在那里某处,但可能需要梳理一下。
2) 使用准备好的语句从多个片段构建查询。传入数组中的各种where条件。不过,它有点倒退(即从您想要的答案开始,然后构建查询)。
There are probably a few ways to approach this:
1) Look for metadata from the query. I know you can get info about the resultset but Im not sure about the original query. Its in there somewhere but might need to be teased out.
2) Use a prepared statement to build your query from pieces. Pass in the various where conditions in an array. Its kind of backwards (IE starting with the answer you want and then building the query) though.