Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This is more for analyzing a query in PHP BEFORE it's sent to the server. Very complicated why im doing this, so i'd rather not go into the reason for this.

In PHP, i need to store the field selections into a php array. So take this query for example:

SELECT user_id,username,DATE(join_datetime) as join_date, (SELECT COUNT(1) FROM foobar WHERE foonum IN (5,4,6) and user_id = users.user_id) as myfoo_count 
FROM users 
WHERE user_id = 123

So, in this case I need to store "user_id,username,DATE(join_datetime) as join_date, (SELECT COUNT(1) FROM foobar WHERE foonum IN (5,4,6) and user_id = users.user_id) as myfoo_count" into an array exploded by a comma (,). So I would get:

array (
  [1] => 'user_id',
  [2] => 'username',
  [3] => 'DATE(join_datetime) as join_date',
  [4] => '(SELECT COUNT(1) FROM foobar WHERE foonum IN (5,4,6) and user_id = users.user_id) as myfoo_count'
)

I have gotten as far as extracting the fields part of the query, but im stuck on trying to explode the fields by comma. The main problem being with subqueries which might have commas in them too (see example).

Thanks for any help!

share|improve this question
1  
Well, its got nothing to do with that. Its a situation which would take me a novel to explain why. Its not bad design or laziness, i just don't want to bore everyone with the uneeded details of the project. –  David Feb 11 '11 at 12:33
1  
X-Ref: SQL parser in PHP? –  hakre Jul 9 at 8:01
 
David, anybody who doubts the usefulness of a parser for a declarative language within PHP isn't worth arguing with. Note, I wrote the PHP SQL parser referenced in the X-Ref. –  Justin Swanhart Oct 6 at 3:50

2 Answers

up vote 0 down vote accepted

You would have to write a parser almost as complex as MySQL's query parser (written in YACC/Bison for C). It's not going to be a regular expression or a little string manipulation. This is a nonregular language, you can't parse them without an actual parser.

You can't just walk through the string finding commas and parentheses either, SQL is much more complex than that. You have expressions within expressions, function calls, conditional logic, etc. all of which can be nested arbitrarily deep with commas and parentheses all over.

http://dev.mysql.com/doc/refman/5.0/en/expressions.html

If you really want to do this with PHP, you have a big job ahead of yourself.

share|improve this answer
 
As noted in the second answer, I wrote an actual SQL parser for PHP because I need to parse and re-write SQL for sharding. It is called PHP-SQL-parser. code.google.com/p/php-sql-parser The parser is used by SugarCRM, is available as a Drupal plugin and more. It is very thorough. –  Justin Swanhart Oct 6 at 3:45

For anyone coming across this question in the future someone has already gone to the trouble of writting an SQL parser in PHP.

At present it supports SELECT, INSERT, UPDATE, DELETE and REPLACE statements.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.