php获取mysql字段名称
在php中查看mysql数据库的信息,可以直接连接mysql,然后调用mysql语句实现。
1、查询mysql数据库/表相关信息的sql语句:
由上述sql语句可以看到,可以使用show full columns来列出字段及详情信息
例如:
打印结果:
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 语句来代替。