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.)
|