Location: PHPKode > scripts > mysql_db class > mysql_db-class/mysql_tools.php
<?PHP
// MYSQL_TOOLS: SQL database structure framework for MYSQL_DB
// Release 1.1 - 06/08/2002
// Released under GNU GPL (see LICENSE for details)

class mysql_tools {
  // Init variables required
  VAR $connobj      = "" ;
  // Data related variables
  VAR $rs ;
  VAR $database ;
  // --------- Private methods  ---------
  // List all tables in a database
  function tbl_list()
    {
    // Determine if we have a valid db open
    if ($GLOBALS[$this->connobj]->db) {
      $this->rs = mysql_list_tables($this->database) ;
      $GLOBALS[$this->connobj]->error_report() ;
      if ($this->rs) {
        $GLOBALS[$this->connobj]->msg = date("d/m/Y - H:i:s") . " - OPERATION O.K.: Listing datbase tables got " . mysql_num_rows($this->rs) . " tables" . "\r\n";
        echo '<table border=1 width=500><tr><th colspan="2">Table list for database ' . $this->database . ' in server ' . $GLOBALS[$this->connobj]->server . '</th></tr>' ;
        if (mysql_num_rows($this->rs)!=0) {
          for ($i=0;$i<mysql_num_rows($this->rs);$i++) {
            echo "<tr><td>".mysql_tablename($this->rs,$i)."</td>" ;
            $aux = mysql_query("select count(*) from ".mysql_tablename($this->rs,$i),$this->db) ;
            $row = mysql_fetch_row($aux) ;
            echo "<td>" . $row[0] . " Records" . "</td></tr>" ;
            mysql_free_result($aux) ;
            unset($row) ;
            unset($aux) ;
            }
        } else {
          echo '<tr><td align="center">No tables in this database</td></tr>' ;
          }
        echo "</table>" ;
        mysql_free_result($this->rs) ;
        }
    } else {
      $GLOBALS[$this->connobj]->msg = date("d/m/Y - H:i:s") . " - OPERATION FAILED: No database open" . "\r\n";
      }
    $GLOBALS[$this->connobj]->debug() ;
    }
  // Lista full information for a given table
  function table_struct($tblname)
    {
    // Determine if we have an open database
    if ($GLOBALS[$this->connobj]->db) {
      $this->rs = @mysql_list_fields($this->database,$tblname) ;
      $GLOBALS[$this->connobj]->error_report() ;
      if ($this->rs) {
        $GLOBALS[$this->connobj]->msg = date("d/m/Y - H:i:s") . " - OPERATION O.K.: Listing table " . $tblname . " information got " . mysql_num_fields($this->rs) . " fields" . "\r\n";
        echo "<table border=1 width=500><tr><th colspan=4>Information from $tblname in database $this->database</th></tr><tr><th>NAME</th><th>TYPE</th><th>SIZE</th><th>FLAGS</th></tr>" ;
        for ($i=0;$i<mysql_num_fields($this->rs);$i++) {
          echo "<tr><td>".mysql_field_name($this->rs,$i)."</td>" ;
          echo "<td>".mysql_field_type($this->rs,$i)."</td>" ;
          echo "<td>".mysql_field_len($this->rs,$i)."</td>" ;
          echo "<td>".mysql_field_flags($this->rs,$i)."</td>" ;
          }
        echo "</table>" ;
        mysql_free_result($this->rs) ;
      } else {
        echo "<table border=1 width=500><tr><th>Information from $tblname in database $this->database</th></tr><tr><td align=center>This table does not exist in this database</td></tr>" ;
        $GLOBALS[$this->connobj]->msg = date("d/m/Y - H:i:s") . " - OPERATION FAILED: Table " . $tblname . " does not exist" . "\r\n";
        }
    } else {
      $GLOBALS[$this->connobj]->msg = date("d/m/Y - H:i:s") . " - OPERATION FAILED: No database open" . "\r\n";
      }
    $GLOBALS[$this->connobj]->debug() ;
    }
  // Full database description
  function db_analyze()
    {
    // Determine if we have an open database
    if ($GLOBALS[$this->connobj]->db) {
      $this->server() ;
      $this->list_tables() ;
      // Get all the table names
      $this->rs = mysql_list_tables($this->database) ;
      if ($this->rs) {
        $this->recordcount = mysql_num_rows($this->rs) ;
        for ($i=0;$i<$this->recordcount;$i++) {
          $names[] = mysql_tablename($this->rs,$i) ;
          }
        mysql_free_result($this->rs) ;
        }
      reset($names) ;
      // now list the structures...
      $this->table_struct(current($names)) ;
      for ($i=0;$i<count($names)-1;$i++) {
        echo "<br><br>" ;
        $this->table_struct(next($names)) ;
        }
    } else {
      $GLOBALS[$this->connobj]->msg = date("d/m/Y - H:i:s") . " - OPERATION FAILED: No database open" . "\r\n";
      }
    $GLOBALS[$this->connobj]->debug() ;
    }
  // Server information
  function server_info()
    {
    printf ("MySQL host info: %s<br>\n", mysql_get_host_info());
    printf ("MySQL server version: %s<br>\n", mysql_get_server_info());
    printf ("MySQL protocol version: %s<br>\n", mysql_get_proto_info());
    printf ("MySQL client info: %s<br>\n", mysql_get_client_info());
    }
  // --------- Public interface ---------
  // Constructor...
  function mysql_tools($connobjname)
    {
    $this->connobj    = $connobjname ;
    $this->database   = $GLOBALS[$connobjname]->database ;
    $this->db         = $GLOBALS[$connobjname]->db ;
    }
  // List all tables from the database
  function list_tables()
    {
    $this->tbl_list() ;
    }
  // Function describe table
  function describe_table($tblname)
    {
    $this->table_struct($tblname) ;
    }
  // Show full database estructure
  function analyze()
    {
    $this->db_analyze() ;
    }
  // Show Server information
  function server()
   {
   $this->server_info() ;
   }
  }
?>
Return current item: mysql_db class