Home » Php » How do I get the MySQL table structure in PHP? Plus a list of all tables?

How do I get the MySQL table structure in PHP? Plus a list of all tables?

Posted by: admin November 29, 2017 Leave a comment

Questions:

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?

Answers:

To get a list of columns for a table, use the DESCRIBE SQL statement. The syntax is as follows:

DESCRIBE TableName

To get a list of tables on the database, use this SQL statement:

SHOW TABLES

Questions:
Answers:
$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/

Questions:
Answers:

To get the CREATE syntax use

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.

See: How to find all the tables in MySQL with specific column names in them?

Questions:
Answers:

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.