Library code snippets

Get metadata on MySQL databases

Getting information about databases if essential if you want to write generic and scalable applications. This code shows you how to get information such as all databases on the server, all tables in each database and all field and field info for each table. Even if you do not need to build on this code, you might want to copy the code which prints out all databases, tables and field information plus examples. Its a great way to get an overview of the tables you are working on for a project.

<?
//getting metadata on MySQL databases

//output the structure of a table
$connection_1 = mysql_connect("localhost");
$fields = mysql_list_fields("cmphp","rights");
for($i=0;$i<mysql_num_fields($fields);$i++) {
   echo mysql_field_name($fields,$i)." (".mysql_field_len($fields,$i).") - ".mysql_field_type($fields,$i)."<br>";
}
mysql_close;

//show the structure of ALL tables in ALL databases on the server
$server_connection_1 = mysql_connect("localhost");
$databases = mysql_query("SHOW DATABASES");
while($database = mysql_fetch_row($databases)) {
   echo '<h2>DATABASE: '.$database[0].'</h2>';
   $database_connection_1 = mysql_select_db($database[0]);
   $tables = mysql_query("SHOW TABLES");
   while($table = mysql_fetch_row($tables)){
       echo '<table border="1" cellpadding="5" width="500">';
       echo '<tr><td colspan="3" bgcolor="silver">TABLE: '.$table[0].'</td></tr>';
       $fields = mysql_list_fields($database[0],$table[0]);
       for($i=0;$i<mysql_num_fields($fields);$i++) {
           echo '<tr>';
           echo '<td>'.mysql_field_name($fields,$i)."</td>";
           echo '<td>'.mysql_field_len($fields,$i)."</td>";
           echo '<td>'.mysql_field_type($fields,$i)."</td>";
           echo '</tr>';
       }
       echo '</table><br>';
   }
}
mysql_close;

//show the nice structure of a particular database
$server_connection_1 = mysql_connect("localhost");
$the_database = "cmphp";
echo '<h2>DATABASE: '.$the_database.'</h2>';
$database_connection_1 = mysql_select_db($the_database);
$tables = mysql_query("SHOW TABLES");
while($table = mysql_fetch_row($tables)){
   echo '<table border="1" cellpadding="5" width="600">';
   echo '<tr><td colspan="4" bgcolor="silver"><b>TABLE: '.$table[0].'</b></td></tr>';
   echo '<tr><td bgcolor="silver">NAME</td><td bgcolor="silver">SIZE</td><td bgcolor="silver">TYPE</td><td bgcolor="silver">EXAMPLE</td></tr>';
   $fields = mysql_list_fields($the_database,$table[0]);
   for($i=0;$i<mysql_num_fields($fields);$i++) {
       echo '<tr>';
       echo '<td>'.mysql_field_name($fields,$i)."</td>";
       echo '<td>'.mysql_field_len($fields,$i)."</td>";
       echo '<td>'.mysql_field_type($fields,$i)."</td>";
       $rows = mysql_query("SELECT ".mysql_field_name($fields,$i)." FROM ".$table[0]." LIMIT 1");
       $row = mysql_fetch_array($rows);
       echo '<td bgcolor="eeeeee">'.$row[0].' </td>';
       echo '</tr>';
   }
   echo '</table><br>';
}
mysql_close;
?>

Comments

  1. 11 Jan 2006 at 17:14

    I try to show my databases but I get this:


    Warning: mysqlfetchrow(): supplied argument is not a valid MySQL result resource


    I use:

    Code:

    $databases = mysqlquery("SHOW DATABASES", $link);
    while($database = mysql
    fetch_row($databases))
    {
      echo '<h2>DATABASE: '.$database[0].'</h2>';
    }


    ??

  2. 01 Jan 1999 at 00:00

    This thread is for discussions of Get metadata on MySQL databases.

Leave a comment

Sign in or Join us (it's free).

Edward Tanguay Edward Tanguay updates his personal web site tanguay.info weekly with code, links, quotes and thoughts on web development. Sign up for the free newsletter.

Related podcasts

  • Scaling Large Web Sites with Joe Stump, Lead Architect at DIGG

    Have you ever wanted to learn how top 100 web sites are architected? Deep Fried Bytes hosts Keith Elder and Chris Woodruff sat down with Joe Stump, Lead Architect at DIGG to discuss scaling large web sites, his life, development experiences and team building. Listen to the showThanks to our gues...

Events coming up

  • Apr 17

    WebTech Conference 2010 - Bulgaria

    Veliko Turnovo, Bulgaria

    6th edition of WebTech conference will be held. A 2 day conference about : - Web Technologies - Blogs and blogging - Web 3.0 - Open Web - Mobile technologies - Internet Business

Want to stay in touch with what's going on? Follow us on twitter!