首先我們需要了解下查詢MySQL數據庫/表相關信息的SQL語句:
復制代碼 代碼如下:
SHOW DATABASES //列出 MySQL Server 數據庫。
SHOW TABLES [FROM db_name] //列出數據庫數據表。
SHOW CREATE TABLES tbl_name //導出數據表結構。
SHOW TABLE STATUS [FROM db_name] //列出數據表及表狀態信息。
SHOW COLUMNS FROM tbl_name [FROM db_name] //列出資料表字段
SHOW FIELDS FROM tbl_name [FROM db_name],DESCRIBE tbl_name [col_name]。
SHOW FULL COLUMNS FROM tbl_name [FROM db_name]//列出字段及詳情
SHOW FULL FIELDS FROM tbl_name [FROM db_name] //列出字段完整屬性
SHOW INDEX FROM tbl_name [FROM db_name] //列出表索引。
SHOW STATUS //列出 DB Server 狀態。
SHOW VARIABLES //列出 MySQL 系統環境變量。
SHOW PROCESSLIST //列出執行命令。
SHOW GRANTS FOR user //列出某用戶權限
由上述SQL語句可以看到,我們可以使用SHOW FULL COLUMNS來列出字段及詳情信息,示例代碼:
復制代碼 代碼如下:
$rescolumns = mysql_query("SHOW FULL COLUMNS FROM ".TB_NAME."") ;
while($row = mysql_fetch_array($rescolumns)){
// echo '字段名稱:'.$row['Field'].'-數據類型:'.$row['Type'].'-注釋:'.$row['Comment'];
// echo '<br/><br/>';
print_r($row);
}
打印結果:
復制代碼 代碼如下:Array ( [0] => id [Field] => id [1] => char(2) [Type] => char(2) [2] => utf8_general_ci [Collation] => utf8_general_ci [3] => NO [Null] => NO [4] => PRI [Key] => PRI [5] => [Default] => [6] => [Extra] => [7] => select,insert,update,references [Privileges] => select,insert,update,references [8] => [Comment] => )
Array ( [0] => title [Field] => title [1] => char(50) [Type] => char(50) [2] => utf8_general_ci [Collation] => utf8_general_ci [3] => YES [Null] => YES [4] => [Key] => [5] => [Default] => [6] => [Extra] => [7] => select,insert,update,references [Privileges] => select,insert,update,references [8] => 建議存儲:標題、姓名等信息 [Comment] => 建議存儲:標題、姓名等信息 )
Array ( [0] => des [Field] => des [1] => varchar(255) [Type] => varchar(255) [2] => utf8_general_ci [Collation] => utf8_general_ci [3] => YES [Null] => YES [4] => [Key] => [5] => [Default] => [6] => [Extra] => [7] => select,insert,update,references [Privileges] => select,insert,update,references [8] => [Comment] => )
…………
補充說明信息:
當然你也可以通過mysql_list_fields — 列出 MySQL 結果中的字段。mysql_list_fields() 取得給定表名的信息,參數是數據庫名和表名,返回一個結果指針。
但是,mysql_list_fields() 函數已過時。最好用 mysql_query() 來發出一條 SHOW COLUMNS FROM table [LIKE 'name'] 的 SQL 語句來代替。詳細可參考PHP幫助文檔:http://www.php.net/manual/zh/function.mysql-list-fields.php