GeezerVison is showing github.com — fixup of example in documentation · phpmyadmin/sql-parser@784940e
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: README.md +13-5Lines changed: 13 additions & 5 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -42,14 +42,22 @@ echo SqlParser\Utils\Formatter::format($query, array('type' => 'html')); |
42 | 42 | ### Parsing and building SQL query |
43 | 43 |
|
44 | 44 | ```php |
45 | | -$parsed = new SqlParser\Parser($query); |
| 45 | +require __DIR__."/vendor/autoload.php"; |
46 | 46 |
|
47 | | -// you can now inspect or change query |
48 | | -var_dump($parser->statements[0]); |
| 47 | +$query1 = "select * from a"; |
| 48 | +$parser = new SqlParser\Parser($query1); |
49 | 49 |
|
50 | | -// and build it again |
| 50 | +// inspect query |
| 51 | +var_dump($parser->statements[0]); // outputs object(SqlParser\Statements\SelectStatement) |
| 52 | + |
| 53 | +// modify query by replacing table a with table b |
| 54 | +$table2 = new \SqlParser\Components\Expression("", "b", "", ""); |
| 55 | +$parser->statements[0]->from[0] = $table2; |
| 56 | + |
| 57 | +// build query again from an array of object(SqlParser\Statements\SelectStatement) to a string |
51 | 58 | $statement = $parser->statements[0]; |
52 | | -$statement->build() |
| 59 | +$query2 = $statement->build(); |
| 60 | +var_dump($query2); // outputs string(19) "SELECT * FROM `b` " |
53 | 61 | ``` |
54 | 62 |
|
55 | 63 | ## More information |
|