2011-04-08 65 views
2

我想在PHP中編寫一個腳本,它將'MySQL Create Table'Query作爲字符串並將列名和它們的數據類型存儲在數組中。如何從'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庫?

感謝

+0

使用ORM,學說可能是 – 2011-04-08 06:37:02

回答

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', '') 
); 
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'; 
+0

不,不,我不能運行查詢,甚至沒有在系統中安裝了MySQL數據庫。我只想編寫一個腳本,它將獲得一個Create table查詢字符串並解析它,並從該查詢中獲取列名稱。我必須寫我自己的字符串解析函數或有任何現有的實用程序? – Awan 2011-04-08 05:55:25

+0

mysql_query();上面的查詢?哦,等待你想要分析,以獲得數據?你需要正則表達式。 – Khez 2011-04-08 06:04:17

+0

您也無法訪問代碼格式? – 2011-04-08 06:56:14

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; 
} 
+0

儘管你的代碼設計很好,但它的實現卻很差 – 2011-04-08 06:49:33

0

什麼你要找的是SQL解析器。這應該幫幫忙: http://www.phpkode.com/scripts/item/sql-parse-convert-to-tree-array/

+0

不,他不是 – 2011-04-08 06:51:44

+0

基於他對Khez的迴應,我相信他確實:「我只想編寫一個腳本,它將獲得一個Create table query string並解析它並從該查詢中獲取列名。「 – soulkphp 2011-04-08 06:56:02

+0

它看起來很有幫助... – Awan 2011-04-08 07:47:52

0

數組只要運行 「遞減$表名」:

$res=mysql_query("desc $tablename", $db_handle); 
while ($r=mysql_fetch_assoc) { 
    print_r($r); 
} 
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 
         ) 

       ) 
     )