List Tables
2003-02-17 00:17:38
Category: php:database
Description: Show and describe all tables in a database.
Author: detour
Viewed: 4633
Rating: (16 votes)


<?php
  // listtables.php by detour@metalshell.com
  //
  // Show and describe all tables in a database.
  //
  // http://www.metalshell.com/
 
 
  $dbname = "metalshell";
  $dbuser = "user";
  $dbpass = "pass";
  $dbhost = "localhost";
 
  echo("<html><head><title>Tables in $dbname</title></head><body>");
  echo('<font face="arial" style="font-weight:bold">');
 
  // Make connection to mysql
  $dbid = @mysql_connect($dbhost, $dbuser, $dbpass);
 
  // Retrieve tables list from the database.
  // A normal result set is stored $dbtables.
  $dbtables = mysql_list_tables($dbname, $dbid);
 
  // mysql_num_rows is used to get the number of rows in a result set,
  $dbtable_num = mysql_num_rows($dbtables);
 
  // Cycle through each table
  for($x=0; $x < $dbtable_num; $x++) {
    // The table indexed at $x
    $current_table = mysql_tablename($dbtables, $x);
    echo("<H1>$current_table</H1>");
 
    // Get all fields from the table $current_table
    $dbfields = mysql_list_fields($dbname, $current_table, $dbid);
    $dbfield_num = mysql_num_fields($dbfields);
 
    // Break apart the field to print it.
    for($y=0; $y < $dbfield_num; $y++) {
      $field_name = mysql_field_name($dbfields, $y);
      $field_type = mysql_field_type($dbfields, $y);
      $field_len  = mysql_field_len($dbfields, $y);
      $field_flag = mysql_field_flags($dbfields, $y);
      echo("$field_name &nbsp;&nbsp;&nbsp;( $field_type of size $field_len ) ");
      echo("&nbsp;&nbsp;FLAGS: $field_flag<br>");
    }
  }
 
  // Close the database connection.
  mysql_close($dbid);
 
  echo('</body></html>');
 
?>