简体   繁体   中英

How to fetch column names from 'MySQL Create Table' Query string?

I want to write a script in PHP which get 'MySQL Create Table' Query as string and store column names and their data types in array.

For example:

input string:

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

output:

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

I don't have database access to executes queries directly. Is there any PHP library for this or Any idea ?

Thanks

using preg_match_all

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

This will generate an array like

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

I don't have database access to executes queries directly. it's gonna be hard to get any data out of a database without that... But you can use:

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';
$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;
}
$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);

result

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
                        )

                )
        )

What you're looking for is a SQL parser. This should help out: http://www.phpkode.com/scripts/item/sql-parse-convert-to-tree-array/

Just run "desc $tablename":

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM