Monday, 6 August 2012

PHP code that displays database structure

 Here is the piece of code that displays database structure.
 

 
<?php
   
   mysql_connect("host_name","user","password") or die(mysql_error());
   mysql_select_db("database_name") or die(mysql_error());



   $result = mysql_query("SHOW TABLES;") or die(mysql_error());  

   while($row = mysql_fetch_array($result)){
  
 echo $row[0]; //print the table name
 
 $result2 = mysql_query("DESCRIBE ".$row[0].";") or die(mysql_error());
 //get details schema for each table

 
 echo "<table border='1' width='70%'>";
 echo "<tr><td>Field</td><td>Type</td><td>Null</td><td>Key</td>
<td>Default</td><td>Extra</td>";

 
 while($row2 = mysql_fetch_array($result2)){
   
  for($i=0; $i<6; $i++){ 
   if($row2[$i] == "" || $row2[$i] == NULL){
    $row2[$i] = " ";
   }
  }   
   
  echo "<tr>";

  echo "<td>".$row2[0]."</td><td>".$row2[1]."</td><td>".$row2[2]."</td>
<td>".$row2[3]."</td><td>".$row2[4]."</td><td>".$row2[5];

  echo "</tr>";
 }
 
 echo "</table>"; 
 echo "<br/>";
}

?>

No comments:

Post a Comment