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?
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.
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.
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
$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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With