Table Administration (Cont.)
May 31, 2002
The displayTable() method executes a select * from <table> command and returns all of the rows in an associative array:
//################################################################## //## Method: displayTable ## //## Parameters: tablename ## //## Return value: array of records in table ## //################################################################## function displayTable($table) { $this->sqlstmt = "select * from ".$table; $queryid = mysql_db_query($this->database,$this->sqlstmt,$this->linkid); if (empty($queryid)) { return 0; } $count = mysql_num_rows($queryid); for ($i=0;$i < $count;$i++) { $tableresults[$i] = mysql_fetch_array ($queryid,MYSQL_ASSOC); } return $tableresults; }
}//end of tableAdmin class
After defining the methods of the tableAdmin class, check that the user is able to establish a connection to the server and that they have selected a database to work with before administering any tables:
include ('chapter5_checklogin.php');
if ($sessiondata[database] == NULL ) { echo "<CENTER>\n"; echo "<BR><BR>\n"; echo "You must use dbAdmin to select a database to work with."; echo "</CENTER>\n";; $nodatabase = TRUE; }
Next, present the user with the submenu of available table administration options:
if ($loggedin == TRUE && !$nodatabase) { echo "<CENTER>\n"; echo "<A HREF=\"chapter5_tableAdmin.php?action=list\""; echo ">List Tables</A>\n"; echo "<A HREF=\"chapter5_tableAdmin.php?action=describe1\""; echo ">Describe Table</A>\n"; echo "<A HREF=\"chapter5_tableAdmin.php?action=drop1\""; echo ">Drop Table</A>\n"; echo "<A HREF=\"chapter5_tableAdmin.php?action=display1\""; echo ">Display Table</A>\n"; echo "</CENTER>\n"; }
You then create a tableAdmin object that you will use to execute any table administration methods:
$myTableAdmin = new tableAdmin($sessiondata[database],$myDBConn->linkid);
We examine the action variable to determine which method to call:
switch($userinfo[action]) {
If the action is list, you display a single-column listing of all the tables in the database:
case 'list': { $tablelistarray = $myTableAdmin->listTables(); displayArray("Tables",$tablelistarray); break; }
If the action is describe, you first ask which table you want to describe, and then call the describe method of your tableAdmin object:
case 'describe1': { displayOneLineForm('chapter5_tableAdmin.php','Table Name', 'in_table','describe2','Describe Table'); break; } case 'describe2': { $descriptionlist = $myTableAdmin->describeTable($userinfo[in_table]); displayResultSet($descriptionlist); break; }
We used the displayOneLineForm() function from the chapter5_common.php file to simplify this script.
In the case of dropTable() and displayTable(), you again prompt for the table name and then call the corresponding method of the tableAdmin object:
case 'drop1': { displayOneLineForm('chapter5_tableAdmin.php','Table Name', 'in_table','drop2','Drop Table'); break; } case 'drop2': { $rc = $myTableAdmin->dropTable($userinfo[in_table]); echo "<BR><BR>\n"; echo "<CENTER>\n"; if ($rc ==TRUE) { echo "Table ".$userinfo[in_table]." was dropped."; } else { echo "Table ".$userinfo[in_table]." could not be dropped."; } echo "</CENTER>\n"; break; } case 'display1': { displayOneLineForm('chapter5_tableAdmin.php','Table Name', 'in_table','display2','Display Table'); break; } case 'display2': { $tableresults = $myTableAdmin->displayTable($userinfo[in_table]); displayResultSet($tableresults); break; } } ?> </BODY> </HTML>
Table Administration
Instant PHP 4
User Administration
|