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.

$query= "describe $table";
$result= mysql_query($query);

$allfields = array();

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

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

OR, collect all field names only:

while($row = mysql_fetch_assoc($result)){
    $allfields[] = $row['Field'];
}

print_r($allfields);

Leave a Reply