Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interpreting SQL with PHP

Tags:

sql

php

So, I need to use PHP to pull out field names and values from the where statement of a sql query, change them, then put the thing back together.

So, say I'm given this string: field='value' AND field2<7 I need to recognize field and field2 as field names, then feed then through the function field() and put them back into the query. Same things with 'value' and 7, but feed them through a value() function. It would also help to know what field each value was associated with.

Is there any way to do this that won't take a few thousand lines to code?

I need it to work with a lot of different possible inputs, including things like: table.field BETWEEN 7 AND 10 or field<7 AND (field2=3 OR field3=4)

like image 421
John Stimac Avatar asked Oct 07 '22 16:10

John Stimac


1 Answers

I've used the php-sql-parser project which is open source and pretty good.

like image 125
mellamokb Avatar answered Oct 10 '22 09:10

mellamokb