# PHP-SQL-Parser: a MySQL dialect parser that hands you clause arrays

> PHP-SQL-Parser is a pure PHP, non validating SQL parser built around the MySQL dialect, returning clause arrays such as SELECT, FROM and WHERE for a defined set of statement types and a token array for everything else.

**greenlion/PHP-SQL-Parser** — A pure PHP SQL (non validating) parser w/ focus on MySQL dialect of SQL

- Repository: https://github.com/greenlion/PHP-SQL-Parser
- Stars: 644 · Forks: 170
- Language: PHP
- License: BSD-3-Clause
- Published: 2026-10-21 · Updated: 2026-10-21 · Language: en
- Canonical page: https://hysenlabs.com/projects/greenlion-php-sql-parser

## A parser that assumes the SQL in front of it is already valid

PHP-SQL-Parser is a pure PHP SQL parser with one explicit stance: it does not validate. The project states that the focus is complete and accurate support for the MySQL dialect, that the focus is not on optimizing for performance, and that it is expected you will present it syntactically valid queries. That single design decision defines the audience. If you are writing a query builder, a migration tool, a report generator or an admin screen that rewrites saved SQL, the parser is there to give you structure, and the correctness of the SQL is your problem. If you are trying to decide whether a query from a user is safe to run, this library is the wrong shape of tool, because a successful parse says nothing about whether the statement is legal or dangerous. The licence is BSD-3-Clause, and the last push to the repository was on 2026-09-21.

## Fourteen statement types get structure, the rest get a token array

The supported set is the core of the library. The documentation lists full support for the MySQL dialect for SELECT, INSERT, UPDATE, DELETE, REPLACE, RENAME, SHOW, SET, DROP, CREATE INDEX, CREATE TABLE, EXPLAIN and DESCRIBE. Every other statement type comes back as a plain array of tokens, and the documentation is honest that this is not as structured as the information available for the listed types. That is a workable contract rather than a defect. A tool that rewrites SELECT statements gets the full array, while a tool that only needs to recognise a statement kind can still work off the token list for anything else. It does mean that coverage in your application is uneven across statement types, and it means the ParserManual on the project wiki is the place to find out what a given statement actually returns.

## Parsing a query and reading the array you get back

The documentation walks through one query and the array it produces. The query in the example is:
```sql
SELECT STRAIGHT_JOIN a, b, c 
  FROM some_table an_alias
 WHERE d > 5;
```
The result is a nested array printed with print_r, and its shape is the whole API in miniature. The top level is keyed by clause, so you get an OPTIONS key holding the query options, here the single entry STRAIGHT_JOIN, then SELECT, FROM and WHERE keys holding lists of expression entries. The repository also ships examples/example.php, which is the fastest way to see the array for your own SQL, and examples/OracleSQLTranslator.php, which suggests the parsing output being used to translate statements between dialects. What you should expect when you run this is a data structure you can walk with ordinary PHP array access, with no object graph to learn.

## expr_type, base_expr, sub_tree and alias do all the work

Four fields carry the meaning in the example output, and they are worth learning before you write any traversal code. expr_type classifies an entry, and in the sample you see colref for the column references a, b, c and d, operator for the greater than sign, and const for the literal 5. base_expr holds the original text of that piece, which is how you recover the exact source fragment for a rewrite. sub_tree is where a nested expression would be attached, so a compound condition appears as a tree rather than as a flat list. alias carries the name a table or column was given, and in the FROM clause you also get table, join_type, ref_type and ref_clause, which is where information about joins lives. The same field set repeats for every clause, which is what makes generic code over the array possible.

## Other dialects work until a reserved word disagrees

The library is not limited to MySQL, but the route to another dialect is a documented one. The reasoning in the documentation is that the MySQL dialect is very close to SQL-92, so the parser should work for most database applications that need a SQL parser. The catch is explicit: if you use another dialect you may want to change the reserved words, and the ParserManual explains how. That is a real difference in effort between dialects. MySQL statements, with their backtick quoting and their own function names, work out of the box, while another vendor's syntax may need the keyword list adjusted, and each keyword change is a place where parsing can start to differ from the server's own view. UNION, subqueries and compound statements are supported regardless of dialect.

## No dependencies, but also no validation and no speed promise

The parser is described as a self contained class with no external dependencies, and it uses a small amount of regex. There is nothing to install alongside it, and no service to run. The trade-off is stated in the same breath. Validation is not part of the job, so a malformed statement does not raise a syntax error from this library, and performance is explicitly not the target of the work. That combination suits the classic use: a batch of statements that already passed through your own code path or your own database layer, where you want to know what a query does, not whether it is legal. It does not suit a pipeline that must handle arbitrary user input, where a parse that succeeds says only that the tokens were recognizable, and where the database server is the only component that can tell you the statement is invalid.

## Version 5 brings MySQL 8, and older branches matter for PHP versions

The release history explains which branch to pick. Version 5.0.0, tagged on 2026-09-19, is described as adding MySQL 8 features. The previous major line reached 4.7.0 on 2024-12-02 with a fix for PHP 8.4, and 4.6.0 was tagged on 2023-03-09. So the current major version is the one that understands MySQL 8 syntax, and the 4.x line is where PHP 8.4 compatibility was addressed. If your application still runs on an older PHP baseline, the branch choice matters more than the feature set, and the 4.x tag history is the place to look. The repository carries phpunit.xml and runtest.sh alongside src/ and tests/, so the test setup is visible in the tree, and a .travis.yml reflects the continuous integration used by the project.

## When the database server should parse the statement instead

There is a genuine alternative approach, and it is worth weighing before you adopt the library. Instead of parsing SQL in PHP, you can let the database server parse it: send the statement and read back the server's own view of it, using the EXPLAIN and DESCRIBE statement types this library itself supports as a hint that servers can be asked what a statement means. The difference is direct. The server returns the plan it will actually execute, which reflects the installed version, the schema and the optimiser, so it is authoritative for that database and that moment. This parser returns a static syntactic structure that is the same on every machine and never talks to a server, which is what you want when you need to inspect SQL offline, transform it, or handle statements you must not execute. If you need the server's judgement, or you want validation, the server route wins. If you need structure without a connection, the PHP array wins.

## Conclusion

PHP-SQL-Parser fits PHP applications that need to read, rewrite or inspect MySQL statements they have already validated, in particular reporting and SQL generation code, because a single self contained class turns a query into a clause array with no dependency to install. It does not fit a validator, since it assumes syntactically valid SQL, and it is not the right tool if you need the statement the server will actually run. Before adopting it, check the fourteen fully supported statement types against your own queries, read the ParserManual on the project wiki for the reserved word changes other dialects need, and note that version 5.0.0 added MySQL 8 features on 2026-09-19.

## FAQ

### Which SQL dialects does PHP-SQL-Parser support?

The focus is the MySQL dialect, with full support for SELECT, INSERT, UPDATE, DELETE, REPLACE, RENAME, SHOW, SET, DROP, CREATE INDEX, CREATE TABLE, EXPLAIN and DESCRIBE. The documentation notes that MySQL is close to SQL-92, so other dialects mostly work, and reserved words can be changed as described in the ParserManual on the project wiki.

### Does PHP-SQL-Parser validate SQL syntax?

No. It is a non validating parser that expects you to present syntactically valid queries, and its stated focus is accurate MySQL dialect support rather than performance.

### How do I install PHP-SQL-Parser?

The project points to the Downloads page on its GitHub wiki for the release files. The parser itself is a self contained class with no external dependencies, using only a small amount of regex, so there is nothing else to install alongside it.

### What does the parsed output look like in PHP-SQL-Parser?

It is a nested array keyed by clause, with entries such as OPTIONS, SELECT, FROM and WHERE. Each expression entry carries expr_type such as colref, operator or const, the original base_expr text, a sub_tree for nested expressions, and an alias where one was given.

### What happened in PHP-SQL-Parser version 5.0.0?

Version 5.0.0 was tagged on 2026-09-19 with MySQL 8 features. The previous major line had 4.7.0 on 2024-12-02 with a fix for PHP 8.4, and 4.6.0 on 2023-03-09.

## Sources

- [greenlion/PHP-SQL-Parser on GitHub](https://github.com/greenlion/PHP-SQL-Parser)
- [Issues](https://github.com/greenlion/PHP-SQL-Parser/issues)
- [License: BSD-3-Clause](https://github.com/greenlion/PHP-SQL-Parser/blob/master/LICENSE)
- [README](https://github.com/greenlion/PHP-SQL-Parser/blob/master/README.md)
- [Releases](https://github.com/greenlion/PHP-SQL-Parser/releases)

---

Hysen Labs editorial analysis, written from the project's own repository and release notes. Cite the canonical page: https://hysenlabs.com/projects/greenlion-php-sql-parser
