2

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

例如:

输入字符串:

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 库为此或任何想法?

谢谢

4

6 回答 6

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', '')
);
于 2011-04-08T06:35:06.273 回答
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';
于 2011-04-08T05:51:53.173 回答
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;
}
于 2011-04-08T06:16:20.743 回答
0

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

于 2011-04-08T06:29:10.220 回答
0

只需运行“desc $tablename”:

$res=mysql_query("desc $tablename", $db_handle);
while ($r=mysql_fetch_assoc) {
    print_r($r);
}
于 2011-04-08T15:44:33.020 回答
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
                        )

                )
        )
于 2017-03-27T04:01:26.790 回答