Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Table Administration

May 31, 2002

Once a user has selected a particular database to work with using the dbAdmin class's selectDB() method, they can work with individual tables within the selected database. The actions associated with table administration are contained in the chapter5_tableAdmin.php file. The script starts out as usual by including the common functions and defining the tableAdmin class.

chapter5_tableAdmin.php

<?php

include ('chapter5_common.php'); 

//##################################################################
//##    Class:          tableAdmin                                ##
//##    Description:    Methods to administer a table             ##
//##################################################################
class tableAdmin
   {
   //member variables
   var $database;
   var $linkid;
   var $sqlstmt;
   var $rc;

   //constructor
   function tableAdmin ($database,$linkid)
      {
      $this->database = $database;
      $this->linkid = $linkid;
      }

The first method in the tableAdmin class is the listTables() method, which uses the PHP MySQL function to query what tables are in the selected database:

//##################################################################
//##    Method:         listTables                                ##
//##    Parameters:     none                                      ##
//##    Return value:   array of table names                      ##
//##################################################################
   function listTables()
      {
      $list = mysql_list_tables ($this->database,$this->linkid);
      $count = mysql_num_rows($list);
      $tablelist = array ($count);
      for ($i=0;$i < $count;$i++)
         {
         $tablelist[$i] = mysql_tablename ($list,$i);
         }
      return $tablelist;
      }

The next method is describeTable(), which details the column descriptions that make up the table:

//##################################################################
//##    Method:         describeTable                             ##
//##    Parameters:     table                                     ##
//##    Return value:   array of table detail                     ##
//##################################################################
   function describeTable($table)
      {
      $this->sqlstmt = "describe ".$table;
      $queryid =
      mysql_db_query
      ($this->database,$this->sqlstmt,$this->linkid);
      if (empty($queryid))
        {
        echo "empty";
        return 0;
        }
      $count = mysql_num_rows($queryid);
      for ($i=0;$i < $count;$i++)
         {
         $describeresults[$i] =
         mysql_fetch_array ($queryid,MYSQL_ASSOC);
         }
      return $describeresults;
      }

The dropTable() method simply takes a table name to be dropped and returns a result code indicating success or failure:

//##################################################################
//##    Method:         dropTable                                 ##
//##    Parameters:     tablename                                 ##
//##    Return value:   return code (TRUE = success)              ##
//##################################################################
   function dropTable($tablename)
      {
      $this->sqlstmt = "drop table ".$tablename;
      $this->rc = mysql_db_query  ($this->database,$this->sqlstmt,$this->linkid);
      return $this->rc;
      }


Note:Color coded lines have been split for display purposes

Database Administration (Cont.)
Instant PHP 4
Table Administration (Cont.)


Up to => Home / Authoring / Languages / PHP / Instant




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers