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:
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');
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.