如何在 PHP 中获取 MySQL 表结构?加上所有表的列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/468458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the MySQL table structure in PHP? Plus a list of all tables?
提问by Teifion
What query do I need to run in PHP to get the structure of a given table in the database? And what query do I need to run to get a list of all the tables?
我需要在 PHP 中运行什么查询才能获取数据库中给定表的结构?我需要运行什么查询来获取所有表的列表?
回答by MrValdez
To get a list of columns for a table, use the DESCRIBE SQL statement. The syntax is as follows:
要获取表的列列表,请使用 DESCRIBE SQL 语句。语法如下:
DESCRIBE TableName
To get a list of tables on the database, use this SQL statement:
要获取数据库上的表列表,请使用以下 SQL 语句:
SHOW TABLES
回答by user866339
$q = mysql_query('DESCRIBE tablename');
while($row = mysql_fetch_array($q)) {
echo "{$row['Field']} - {$row['Type']}\n";
}
found it at http://www.electrictoolbox.com/mysql-table-structure-describe/
在http://www.electrictoolbox.com/mysql-table-structure-describe/找到它
回答by Ken
To get the CREATE syntax use
要获得 CREATE 语法,请使用
SHOW CREATE TABLE table_name;
Also take a look in the information_schema database. Lots of very useful information about your databases, tables, indexes, etc.
另请查看 information_schema 数据库。关于您的数据库、表、索引等的许多非常有用的信息。
See: How to find all the tables in MySQL with specific column names in them?
回答by rpayanm
For get comments of the fields you can use:
要获取字段的注释,您可以使用:
SHOW FULL COLUMNS FROM table_name;
Notice keyword FULL, this is what makes MySQL to include privileges and comments info into the response.
注意关键字 FULL,这使 MySQL 在响应中包含权限和注释信息。

