Parse SELECT clause of SQL queries into a PHP array

后端 未结 2 692
小蘑菇
小蘑菇 2020-12-07 03:48

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

2条回答
  •  爱一瞬间的悲伤
    2020-12-07 04:37

    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.

提交回复
热议问题