MySQL Database Php

drill.php





#
#Modify as needed or desired
#
$TESTING = false;
error_reporting( ($TESTING ? E_ALL : 0) );
#
# Initialize variables used in this script
#
$thisPAGE = $_SERVER["PHP_SELF"];
$thisHILITE = "LightGreen";
#
# MySQL login information
#
$UserName = "";
$UserPass = "";
$dbHost = "localhost";
#
#Connect to the database and get a database handle
#
$dbh = mysql_pconnect ("$dbHost", "$UserName", "$UserPass") or die ("Can't connect to server");
#
# Assign URL Parameters to Variables
#
$thisDATABASE = (isset($_REQUEST["db"]) ? $_REQUEST["db"] : "");
$thisTABLE = (isset($_REQUEST["tbl"]) ? $_REQUEST["tbl"] : "");
$thisACTION = (isset($_REQUEST["axn"]) ? $_REQUEST["axn"] : "");
#
# Start buffering output
#
ob_start();
print "";
# display the main heading
print "";
#
# Get a list of available databases on server
#
$dbh_rs = mysql_query("show databases");
print "";
ENDIF;
print "";
#
# User requested a list of tables
#
IF ($thisACTION == "tbl") :
print "";
#
# User requested field information from a table
#
ELSEIF ($thisACTION== "fld") :
#
# Make sure that a database is selected
#
IF ($thisDATABASE == "") :
print "Database name not given: A list of tables cannot be displayed.";
EXIT;
ENDIF;
#
# Make sure that a table is selected
#
IF ($thisTABLE == "") :
print "Table name not given: A list of fields cannot be displayed.";
EXIT;
ENDIF;
#
# Get a list of tables for navigation purposes
#
$dbh_rs = mysql_query("show tables from $thisDATABASE");
#
# If the query succeeded, display the results
#
IF ($dbh_rs AND mysql_num_rows($dbh_rs)) :
print "";
#
# Otherwise, display appropriate message
#
ELSE :
print "";
ENDIF;
print "";
ENDIF;
print "
DB-DRILL : $rootName @ $dbHost : $thisDATABASE : $thisTABLE
";
#
# If the query succeeded, display the results
#
IF ($dbh_rs AND mysql_num_rows($dbh_rs)) :
print "";
print "";
#
# Get each database a row at a time
#
WHILE ( list($thisDB) = mysql_fetch_row($dbh_rs) ) :
# highlight the "current" database
$bgColor = ($thisDB == $thisDATABASE ? "bgcolor='$thisHILITE'" : "");
print "";
ENDWHILE;
print "
Databases
$thisDB
";
#
# Otherwise, display appropriate message
#
ELSE :
print "
No databases found on system server.
";
#
# Make sure that a database is selected
#
IF ($thisDATABASE == "") :
print "Database name not given: A list of tables cannot be displayed.";
EXIT;
ENDIF;
print "";
print "";
print "";
print "";
print "";
print "";
print "";
print "";
print "";
print "";
print "";
print "";
#
# Get a list of tables and related information
#
$dbh_rs = mysql_query("show table status from $thisDATABASE");
#
# If the query succeeded, display the results
#
IF ($dbh_rs AND mysql_num_rows($dbh_rs)) :
#
# Get the tables information a row at a time
#
$loop = 0;
WHILE ($thisROW = mysql_fetch_row($dbh_rs)) :
# alternate background color for each row
$thisBG = ($loop++%2 ? "white" : "e0e0e0");
# display table information
print "";
print "";
print "";
print "";
print "";
print "";
print "";
print "";
print "";
print "";
ENDWHILE;
#
# Otherwise, display appropriate message
#
ELSE :
print "";
ENDIF;
print "
Tables in DB: $thisDATABASE
Table Type RowFmt Recs FileLen Created LUpdate LCheck
" . $thisROW[0] . " " . $thisROW[ 1] . " " . $thisROW[ 2] . " " . $thisROW[ 3] . " " . $thisROW[ 5] . " " . $thisROW[10] . " " . $thisROW[11] . " " . $thisROW[12] . "
No tables were found in this database.
";
print "
";
print "";
print "";
# Retrieve table information a row at a time
WHILE (list($tblName) = mysql_fetch_row($dbh_rs)) :
# Highlite the "current" table
$bgColor = ($tblName == $thisTABLE ? "bgcolor='$thisHILITE'" : "");
print "";
ENDWHILE;
print "
Tables
$tblName
";
print "
No tables were found in this database.
";
#
# Get fields list from selected table
#
$dbh_rs = mysql_query("show fields from $thisDATABASE.$thisTABLE");
print "";
#
# If the query succeeded, display the results
#
IF ($dbh_rs) :
print "";
print "";
print "";
print "";
print "";
print "";
print "";
print "";
print "";
print "";
$loop = 0;
#
# Get/Display field information a row at a time
#
WHILE ( list ($col_name, $col_type, $col_null, $col_key, $col_default, $col_extra) = mysql_fetch_row($dbh_rs) ) :
# alternate background color for each row
$thisBG = ($loop++%2 ? "f0f0f0" : "e0e0e0");
# display field information
print "";
print "";
print "";
print "";
print "";
print "";
print "";
print "";
ENDWHILE;
ENDIF;
#
# Get list of indices associated with the selected table
#
$dbh_rs = mysql_query("show index from $thisDATABASE.$thisTABLE");
#
# If the query succeeded, display the results
#
IF ($dbh_rs) :
print "";
print "";
print "";
print "";
print "";
print "";
print "";
print "";
print "";
print "";
print "";
print "";
print "";
#
# If no indices available, display appropriate message
#
IF (mysql_num_rows($dbh_rs) == 0) :
print "";
#
# Otherwise, display index/key information
#
ELSE :
$old_idx_name = "";
WHILE ( list ($idx_table, $idx_unique, $idx_name, $idx_seq, $idx_col, $idx_collate, $idx_card, $idx_sub, $idx_packed, $idx_null, $idx_type, $idx_comment) = mysql_fetch_row($dbh_rs) ) :
# Prepare field information for "readability"
$idx_unique = ($idx_unique ? "False" : "True" );
$idx_comment = ($idx_comment == "" ? "None" : $idx_comment);
$idx_card = ($idx_card == "" ? "0" : $idx_card );
$idx_null = ($idx_null == "" ? "NO" : $idx_null );
# suppress repetition of index name in case of multiple field keys
$thisIDXname = ($idx_name == $old_idx_name ? " " : $idx_name);
$old_idx_name = $idx_name;
# alternate background color for each row
$thisBG = ($loop++%2 ? "f0f0f0" : "e0e0e0");
# display index information
print "";
print "";
print "";
print "";
print "";
print "";
print "";
print "";
print "";
print "";
print "";
print "";
ENDWHILE;
ENDIF;
ENDIF;
#
# Display the table creation script for this table
#
$dbh_rs = mysql_query("show create table $thisDATABASE.$thisTABLE");
#
# If the query succeeded, display the results
#
IF ($dbh_rs) :
# display header
print "";
# track index name
$old_idx_name = "";
# Fetch the code generated by server
list ($cre_table, $cre_code) = mysql_fetch_row($dbh_rs);
# "Format" the code and print it
$cre_code = str_replace(chr(10), "
", $cre_code);
print "";
ENDIF;
print "
Table: $thisTABLE
F I E L D S
Name Type Null Key Default Attributes
$col_name $col_type $col_null $col_key $col_default $col_extra
I N D I C E S
Name Type Unique? Column Seq Collate Cardinality Packed Null? Comment
No index has been defined for this table.
$thisIDXname $idx_type $idx_unique $idx_col $idx_seq $idx_collate $idx_card $idx_packed $idx_null $idx_comment
CREATE SYNTAX
$cre_code
";
print "
";
#
# Flush the output buffer to the user's browser
#
ob_end_flush();
?>