To print the table structure of MySQL table in PHP

You can print the table structure of MySQL table in PHP using simple php script. All you need to do is to make a ‘describe table’ call and process it using php script, similar to one you use to print or get records from a mysql table. This can prove to be a very useful piece of code in, case where you do not have access to a mysql tool like phpmyadmin to view the table structure of mysql table.

[php]$query= "describe $table";
$result= mysql_query($query);

$allfields = array();[/php]

To get all field information together including field name, type, default etc.:

[php]while($row = mysql_fetch_assoc($result)){
$allfields[] = $row;
}[/php]

OR, collect all field names only:

[php]while($row = mysql_fetch_assoc($result)){
$allfields[] = $row[‘Field’];
}

print_r($allfields);[/php]

Leave a Reply

Your email address will not be published. Required fields are marked *