首先我們需要了解下查詢MySQL數(shù)據(jù)庫/表相關信息的SQL語句:
代碼如下:
SHOW DATABASES //列出 MySQL Server 數(shù)據(jù)庫。
SHOW TABLES [FROM db_name] //列出數(shù)據(jù)庫數(shù)據(jù)表。
SHOW CREATE TABLES tbl_name //導出數(shù)據(jù)表結構。
SHOW TABLE STATUS [FROM db_name] //列出數(shù)據(jù)表及表狀態(tài)信息。
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 狀態(tài)。
SHOW VARIABLES //列出 MySQL 系統(tǒng)環(huán)境變量。
SHOW PROCESSLIST //列出執(zhí)行命令。
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'].'-數(shù)據(jù)類型:'.$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() 取得給定表名的信息,參數(shù)是數(shù)據(jù)庫名和表名,返回一個結果指針。
但是,mysql_list_fields() 函數(shù)已過時。最好用 mysql_query() 來發(fā)出一條 SHOW COLUMNS FROM table [LIKE 'name'] 的 SQL 語句來代替。詳細可參考PHP幫助文檔:PHP: mysql_list_fields - Manua
更多信息請查看IT技術專欄