Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

mysql

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?

like image 485
Teifion Avatar asked Jan 22 '09 08:01

Teifion


People also ask

How can I get a list of all tables in MySQL?

To get a list of the tables in a MySQL database, use the mysql client tool to connect to the MySQL server and run the SHOW TABLES command. The optional FULL modifier will show the table type as a second output column.

How can you view the structure of table in PHP?

So, first, we must connect to the database. After we do this, we create a variable named $tablestructure. This $tablestructure variable is set equal to a mysql_query() function that has the parameter "EXPLAIN table_name". This MySQL query is a query to show the structure of a table.


2 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 
like image 69
MrValdez Avatar answered Sep 20 '22 13:09

MrValdez


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

like image 22
user866339 Avatar answered Sep 20 '22 13:09

user866339