如何从'MySQL Create Table'查询字符串中获取列名?

时间:2011-04-08 05:45:43

标签: php mysql string-parsing

我想在PHP中编写一个脚本,将“MySQL Create Table”查询作为字符串,并将列名称及其数据类型存储在数组中。

例如:

输入字符串:

CREATE TABLE `test` (
`col1` INT( 10 ) NOT NULL ,
`col2` VARCHAR( 50 ) NOT NULL ,
`col3` DATE NOT NULL
) ENGINE = MYISAM ;

输出:

array(
    array( 'name'=>'col1', 'type'=>'INT', 'size'=>'10' ),
    array( 'name'=>'col2', 'type'=>'VARCHAR', 'size'=>'50' ),
    array( 'name'=>'col3', 'type'=>'DATE', 'size'=>'' )
);

我没有数据库访问权来直接执行查询。有没有这个或任何想法的PHP库?

由于

6 个答案:

答案 0 :(得分:5)

使用preg_match_all

preg_match_all("/`(.+)` (\w+)\(? ?(\d*) ?\)?/", $sql, $_matches, PREG_SET_ORDER);

这将生成一个类似

的数组
array(
  array('`col1` INT( 10 )', 'col1', 'INT', '10'),
  array('`col2` VARCHAR( 50 )', 'col2', 'VARCHAR', '50'),
  array('`col3` DATE ', 'col3', 'DATE', '')
);

答案 1 :(得分:2)

I don't have database access to executes queries directly.如果没有这个数据,很难从数据库中获取任何数据......但你可以使用:

SELECT column_name as name, data_type as type, column_type as typeSize 
  FROM information_schema.columns 
  WHERE table_schema='db_name' AND table_name='table_name';

答案 2 :(得分:1)

$query = "SELECT column_name as name, data_type as type, column_type as typeSize 
            FROM information_schema.columns 
            WHERE table_schema='db_name' AND table_name='table_name'";
$viewArr=array();
$tempArr=array();
$resultSet=mysql_query($query) or trigger_error(mysql_error());
while($row=mysql_fetch_array($resultSet)) {
    $viewArr[] = $row;
}

答案 3 :(得分:0)

您正在寻找的是SQL解析器。这应该有助于: http://www.phpkode.com/scripts/item/sql-parse-convert-to-tree-array/

答案 4 :(得分:0)

只需运行“desc $ tablename”:

$res=mysql_query("desc $tablename", $db_handle);
while ($r=mysql_fetch_assoc) {
    print_r($r);
}

答案 5 :(得分:0)

$query = "CREATE TABLE `MyTable` ( `id` INT(10) NOT NULL AUTO_INCREMENT, `name` VARCHAR(64) NOT NULL DEFAULT '', `parent` INT(10) NOT NULL DEFAULT '0', PRIMARY KEY (`id`)) ENGINE=MYISAM AUTO_INCREMENT=4 DEFAULT CHARSET=UTF8";

if (preg_match("/^(CREATE TABLE)+ (IF NOT EXISTS )?`([\S]+)\` \((.*)\) (.*)$/", $query, $_matches) ) {
  $table_name = $_matches[3];
  $fields = $_matches[4];
  if (preg_match_all("/`([\S^`]+)` (\w+)\(? ?(\d*) ?\)?/", $fields, $__matches, PREG_SET_ORDER)) {
    $tables[$table_name] = $__matches;
  }
}; 
print_r($tables);

结果

Array
        (
            [MyTable] => Array
                (
                    [0] => Array
                        (
                            [0] => `id` INT(10)
                            [1] => id
                            [2] => INT
                            [3] => 10
                        )

                    [1] => Array
                        (
                            [0] => `name` VARCHAR(64)
                            [1] => name
                            [2] => VARCHAR
                            [3] => 64
                        )

                    [2] => Array
                        (
                            [0] => `parent` INT(10)
                            [1] => parent
                            [2] => INT
                            [3] => 10
                        )

                )
        )