如何在PHP中获取MySQL表结构?加上所有表格的清单?

时间:2022-06-06 09:13:27

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中运行什么查询来获取数据库中给定表的结构?我需要运行什么查询来获取所有表的列表?

4 个解决方案

#1


43  

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

#2


22  

$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/

#3


19  

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?

请参阅:如何查找MySQL中具有特定列名的所有表?

#4


3  

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将特权和注释信息包含在响应中的原因。

#1


43  

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

#2


22  

$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/

#3


19  

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?

请参阅:如何查找MySQL中具有特定列名的所有表?

#4


3  

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将特权和注释信息包含在响应中的原因。