解析SQL查询PHP [英] Parsing sql query PHP
本文介绍了解析SQL查询PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将一个SQL查询解析为一个数组.我不知道代码.
I want to parse a SQL query into an array. I could not figure out the code.
示例:
$sql_query = "SELECT id, login, pass FROM users WHERE id=3, login=faforty ORDER DESC LIMIT 3"';
,此sql查询应如下所示:
and this sql query should be as follows:
$data = array();
$data['select'] = array('id', 'login', 'pass');
$data['from'] = array('id' => 3, 'login' => 'faforty');
$data['order'] = 'desc';
$data['limit'] = 3;
查询可能有所不同.
推荐答案
使用SQL解析器. http://code.google.com/p/php-sql-parser/
Use an SQL parser. http://code.google.com/p/php-sql-parser/
从此示例中复制粘贴:
Copy paste from this example:
<?php
require_once('php-sql-parser.php');
$parser=new PHPSQLParser('SELECT a FROM some_table an_alias WHERE d > 5;', true);
print_r($parser->parsed);
示例输出:
Array
(
[SELECT] => Array
(
[0] => Array
(
[expr_type] => colref
[alias] =>
[base_expr] => a
[sub_tree] =>
[position] => 8
)
)
[FROM] => Array
(
[0] => Array
(
[expr_type] => table
[table] => some_table
[alias] => Array
(
[as] =>
[name] => an_alias
[base_expr] => an_alias
[position] => 29
)
[join_type] => JOIN
[ref_type] =>
[ref_clause] =>
[base_expr] => some_table an_alias
[sub_tree] =>
[position] => 18
)
)
[WHERE] => Array
(
[0] => Array
(
[expr_type] => colref
[base_expr] => d
[sub_tree] =>
[position] => 45
)
[1] => Array
(
[expr_type] => operator
[base_expr] => >
[sub_tree] =>
[position] => 47
)
[2] => Array
(
[expr_type] => const
[base_expr] => 5
[sub_tree] =>
[position] => 49
)
)
)
这篇关于解析SQL查询PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文