\n"; echo "\n"; echo "Mysql interface\n"; echo "\n"; echo "\n"; echo "
\n"; echo "
\n"; echo "
\n"; echo "

Mysql Interface v1.0

\n"; echo "
\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "
Username
Password

\n"; echo "\n"; echo "
\n"; echo "

\n"; echo "
\n"; echo "
\n"; echo "


\n"; echo "\n"; echo "Copyright © 2005\n
"; echo "
\n"; echo "
\n"; echo "\n"; echo "\n"; } function logon_submit() { global $username, $password, $PHP_SELF; setcookie( "mysql_web_admin_username", $username ); setcookie( "mysql_web_admin_password", $password ); echo ""; echo ""; echo ""; echo ""; echo ""; } function echoQueryResult() { global $queryStr, $errMsg; if( $errMsg == "" ) $errMsg = "Success"; if( $queryStr != "" ) { echo "\n"; echo "\n"; echo "\n"; echo "
Query$queryStr
Result$errMsg

\n"; } } function listDatabases() { global $mysqlHandle, $PHP_SELF; echo "

Database List

\n"; echo "
\n"; echo "\n"; echo "\n"; echo "\n"; echo "
\n"; echo "
\n"; echo "\n"; $pDB = mysql_list_dbs( $mysqlHandle ); $num = mysql_num_rows( $pDB ); for( $i = 0; $i < $num; $i++ ) { $dbname = mysql_dbname( $pDB, $i ); echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; } echo "
$dbnameTableDropDump
\n"; } function createDatabase() { global $mysqlHandle, $dbname, $PHP_SELF; mysql_create_db( $dbname, $mysqlHandle ); listDatabases(); } function dropDatabase() { global $mysqlHandle, $dbname, $PHP_SELF; mysql_drop_db( $dbname, $mysqlHandle ); listDatabases(); } function listTables() { global $mysqlHandle, $dbname, $PHP_SELF; echo "

Table List

\n"; echo "

$dbname

\n"; echoQueryResult(); echo "
\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "
\n"; echo "
\n"; echo "\n"; echo "\n"; echo "\n"; //echo "
"; echo "\n"; echo "
\n"; echo "
\n"; $pTable = mysql_list_tables( $dbname ); if( $pTable == 0 ) { $msg = mysql_error(); echo "

Error : $msg

\n"; return; } $num = mysql_num_rows( $pTable ); echo "\n"; for( $i = 0; $i < $num; $i++ ) { $tablename = mysql_tablename( $pTable, $i ); echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; } echo "
\n"; echo "$tablename\n"; echo "\n"; echo "Schema\n"; echo "\n"; echo "Data\n"; echo "\n"; echo "Drop\n"; echo "\n"; echo "Dump\n"; echo "
"; } function createTable() { global $mysqlHandle, $dbname, $tablename, $PHP_SELF, $queryStr, $errMsg; $queryStr = "CREATE TABLE $tablename ( no INT )"; mysql_select_db( $dbname, $mysqlHandle ); mysql_query( $queryStr, $mysqlHandle ); $errMsg = mysql_error(); listTables(); } function dropTable() { global $mysqlHandle, $dbname, $tablename, $PHP_SELF, $queryStr, $errMsg; $queryStr = "DROP TABLE $tablename"; mysql_select_db( $dbname, $mysqlHandle ); mysql_query( $queryStr, $mysqlHandle ); $errMsg = mysql_error(); listTables(); } function viewSchema() { global $mysqlHandle, $dbname, $tablename, $PHP_SELF, $queryStr, $errMsg; echo "

Table Schema

\n"; echo "

$dbname > $tablename

\n"; echoQueryResult(); echo "Add Field | \n"; echo "View Data\n"; echo "
\n"; $pResult = mysql_db_query( $dbname, "SHOW fields FROM $tablename" ); $num = mysql_num_rows( $pResult ); echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; for( $i = 0; $i < $num; $i++ ) { $field = mysql_fetch_array( $pResult ); echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; $fieldname = $field["Field"]; echo "\n"; echo "\n"; echo "\n"; } echo "
FieldTypeNullKeyDefaultExtraAction
".$field["Field"]."".$field["Type"]."".$field["Null"]."".$field["Key"]."".$field["Default"]."".$field["Extra"]."EditDrop
\n"; } function manageField( $cmd ) { global $mysqlHandle, $dbname, $tablename, $fieldname, $PHP_SELF; if( $cmd == "add" ) echo "

Add Field

\n"; else if( $cmd == "edit" ) { echo "

Edit Field

\n"; $pResult = mysql_db_query( $dbname, "SHOW fields FROM $tablename" ); $num = mysql_num_rows( $pResult ); for( $i = 0; $i < $num; $i++ ) { $field = mysql_fetch_array( $pResult ); if( $field["Field"] == $fieldname ) { $fieldtype = $field["Type"]; $fieldkey = $field["Key"]; $fieldextra = $field["Extra"]; $fieldnull = $field["Null"]; $fielddefault = $field["Default"]; break; } } $type = strtok( $fieldtype, " (,)\n" ); if( strpos( $fieldtype, "(" ) ) { if( $type == "enum" | $type == "set" ) { $valuelist = strtok( " ()\n" ); } else { $M = strtok( " (,)\n" ); if( strpos( $fieldtype, "," ) ) $D = strtok( " (,)\n" ); } } } echo "

$dbname > $tablename

\n"; echo "
\n"; if( $cmd == "add" ) echo "\n"; else if( $cmd == "edit" ) { echo "\n"; echo "\n"; } echo "\n"; echo "\n"; echo "

Name

\n"; echo "

\n"; ?>

Type

* `M' indicates the maximum display size.
* `D' applies to floating-point types and indicates the number of digits following the decimal point.
Type M  D unsignedzerofillbinary
>TINYINT (-128 ~ 127) O   O O  
>SMALLINT (-32768 ~ 32767) O   O O  
>MEDIUMINT (-8388608 ~ 8388607) O   O O  
>INT (-2147483648 ~ 2147483647) O   O O  
>BIGINT (-9223372036854775808 ~ 9223372036854775807) O   O O  
>FLOAT O O   O  
>DOUBLE O O   O  
>DECIMAL(NUMERIC) O O   O  
>DATE (1000-01-01 ~ 9999-12-31, YYYY-MM-DD)          
>DATETIME (1000-01-01 00:00:00 ~ 9999-12-31 23:59:59, YYYY-MM-DD HH:MM:SS)          
>TIMESTAMP (1970-01-01 00:00:00 ~ 2106..., YYYYMMDD[HH[MM[SS]]]) O        
>TIME (-838:59:59 ~ 838:59:59, HH:MM:SS)          
>YEAR (1901 ~ 2155, 0000, YYYY)          
>CHAR O       O
>VARCHAR O       O
>TINYTEXT (0 ~ 255)          
>TEXT (0 ~ 65535)          
>MEDIUMTEXT (0 ~ 16777215)          
>LONGTEXT (0 ~ 4294967295)          
>TINYBLOB (0 ~ 255)          
>BLOB (0 ~ 65535)          
>MEDIUMBLOB (0 ~ 16777215)          
>LONGBLOB (0 ~ 4294967295)          
>ENUM
value list
>SET
value list
MDunsignedzerofillbinaryvalue list (ex: 'apple', 'orange', 'banana')
> > > > > >

Flags

not nulldefault valueauto incrementprimary key
> > > >

\n"; else if( $cmd == "edit" ) echo "\n"; echo "\n"; echo "

\n"; } function manageField_submit( $cmd ) { global $mysqlHandle, $dbname, $tablename, $old_name, $name, $type, $PHP_SELF, $queryStr, $errMsg, $M, $D, $unsigned, $zerofill, $binary, $not_null, $default_value, $auto_increment, $primary_key, $valuelist; if( $cmd == "add" ) $queryStr = "ALTER TABLE $tablename ADD $name "; else if( $cmd == "edit" ) $queryStr = "ALTER TABLE $tablename CHANGE $old_name $name "; if( $M != "" ) if( $D != "" ) $queryStr .= "$type($M,$D) "; else $queryStr .= "$type($M) "; else if( $valuelist != "" ) { $valuelist = stripslashes( $valuelist ); $queryStr .= "$type($valuelist) "; } else $queryStr .= "$type "; $queryStr .= "$unsigned $zerofill $binary "; if( $default_value != "" ) $queryStr .= "DEFAULT '$default_value' "; $queryStr .= "$not_null $auto_increment"; mysql_select_db( $dbname, $mysqlHandle ); mysql_query( $queryStr, $mysqlHandle ); $errMsg = mysql_error(); // key change $keyChange = false; $result = mysql_query( "SHOW KEYS FROM $tablename" ); $primary = ""; while( $row = mysql_fetch_array($result) ) if( $row["Key_name"] == "PRIMARY" ) { if( $row[Column_name] == $name ) $keyChange = true; else $primary .= ", $row[Column_name]"; } if( $primary_key == "PRIMARY KEY" ) { $primary .= ", $name"; $keyChange = !$keyChange; } $primary = substr( $primary, 2 ); if( $keyChange == true ) { $q = "ALTER TABLE $tablename DROP PRIMARY KEY"; mysql_query( $q ); $queryStr .= "
\n" . $q; $errMsg .= "
\n" . mysql_error(); $q = "ALTER TABLE $tablename ADD PRIMARY KEY( $primary )"; mysql_query( $q ); $queryStr .= "
\n" . $q; $errMsg .= "
\n" . mysql_error(); } viewSchema(); } function dropField() { global $mysqlHandle, $dbname, $tablename, $fieldname, $PHP_SELF, $queryStr, $errMsg; $queryStr = "ALTER TABLE $tablename DROP COLUMN $fieldname"; mysql_select_db( $dbname, $mysqlHandle ); mysql_query( $queryStr , $mysqlHandle ); $errMsg = mysql_error(); viewSchema(); } function viewData( $queryStr ) { global $mysqlHandle, $dbname, $tablename, $PHP_SELF, $errMsg, $page, $rowperpage, $orderby; echo "

Data in Table

\n"; if( $tablename != "" ) echo "

$dbname > $tablename

\n"; else echo "

$dbname

\n"; $queryStr = stripslashes( $queryStr ); if( $queryStr == "" ) { $queryStr = "SELECT * FROM $tablename"; if( $orderby != "" ) $queryStr .= " ORDER BY $orderby"; echo "Add Data | \n"; echo "Schema\n"; } $pResult = mysql_db_query( $dbname, $queryStr ); $errMsg = mysql_error(); $GLOBALS[queryStr] = $queryStr; if( $pResult == false ) { echoQueryResult(); return; } if( $pResult == 1 ) { $errMsg = "Success"; echoQueryResult(); return; } echo "
\n"; $row = mysql_num_rows( $pResult ); $col = mysql_num_fields( $pResult ); if( $row == 0 ) { echo "No Data Exist!"; return; } if( $rowperpage == "" ) $rowperpage = 20; if( $page == "" ) $page = 0; else $page--; mysql_data_seek( $pResult, $page * $rowperpage ); echo "\n"; echo "\n"; for( $i = 0; $i < $col; $i++ ) { $field = mysql_fetch_field( $pResult, $i ); echo "\n"; } echo "\n"; echo "\n"; for( $i = 0; $i < $rowperpage; $i++ ) { $rowArray = mysql_fetch_row( $pResult ); if( $rowArray == false ) break; echo "\n"; $key = ""; for( $j = 0; $j < $col; $j++ ) { $data = $rowArray[$j]; $field = mysql_fetch_field( $pResult, $j ); if( $field->primary_key == 1 ) $key .= "&" . $field->name . "=" . $data; if( strlen( $data ) > 20 ) $data = substr( $data, 0, 20 ) . "..."; $data = htmlspecialchars( $data ); echo "\n"; } if( $key == "" ) echo "\n"; else { echo "\n"; echo "\n"; } echo "\n"; } echo "
"; echo "".$field->name."\n"; echo "Action
\n"; echo "$data\n"; echo "no KeyEditDelete
\n"; echo "\n"; echo "
\n"; echo "\n"; echo ($page+1)."/".(int)($row/$rowperpage+1)." page"; echo "\n"; echo " | "; if( $page > 0 ) { echo "Prev\n"; } else echo "Prev"; echo " | "; if( $page < ($row/$rowperpage)-1 ) { echo "Next\n"; } else echo "Next"; echo " | "; if( $row > $rowperpage ) { echo "\n"; echo "\n"; } echo "
\n"; echo "
\n"; } function manageData( $cmd ) { global $mysqlHandle, $dbname, $tablename, $PHP_SELF; if( $cmd == "add" ) echo "

Add Data

\n"; else if( $cmd == "edit" ) { echo "

Edit Data

\n"; $pResult = mysql_list_fields( $dbname, $tablename ); $num = mysql_num_fields( $pResult ); $key = ""; for( $i = 0; $i < $num; $i++ ) { $field = mysql_fetch_field( $pResult, $i ); if( $field->primary_key == 1 ) if( $field->numeric == 1 ) $key .= $field->name . "=" . $GLOBALS[$field->name] . " AND "; else $key .= $field->name . "='" . $GLOBALS[$field->name] . "' AND "; } $key = substr( $key, 0, strlen($key)-4 ); mysql_select_db( $dbname, $mysqlHandle ); $pResult = mysql_query( $queryStr = "SELECT * FROM $tablename WHERE $key", $mysqlHandle ); $data = mysql_fetch_array( $pResult ); } echo "

$dbname > $tablename

\n"; echo "
\n"; if( $cmd == "add" ) echo "\n"; else if( $cmd == "edit" ) echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; $pResult = mysql_db_query( $dbname, "SHOW fields FROM $tablename" ); $num = mysql_num_rows( $pResult ); $pResultLen = mysql_list_fields( $dbname, $tablename ); for( $i = 0; $i < $num; $i++ ) { $field = mysql_fetch_array( $pResult ); $fieldname = $field["Field"]; $fieldtype = $field["Type"]; $len = mysql_field_len( $pResultLen, $i ); echo ""; echo ""; echo ""; echo "\n"; $value = htmlspecialchars($data[$i]); if( $cmd == "add" ) { $type = strtok( $fieldtype, " (,)\n" ); if( $type == "enum" || $type == "set" ) { echo "\n"; } else { if( $len < 40 ) echo "\n"; else echo "\n"; } else { if( $len < 40 ) echo "\n"; else echo ""; } echo "
NameTypeFunctionData
$fieldname".$field["Type"]."\n"; echo "\n"; echo "\n"; if( $type == "enum" ) echo "\n"; echo strtok( "'" ); while( $str = strtok( "'" ) ) { echo "\n"; } } else if( $cmd == "edit" ) { $type = strtok( $fieldtype, " (,)\n" ); if( $type == "enum" || $type == "set" ) { echo "\n"; if( $type == "enum" ) echo "\n"; echo strtok( "'" ); while( $str = strtok( "'" ) ) { if( $value == $str ) echo "\n"; } } echo "

\n"; if( $cmd == "add" ) echo "\n"; else if( $cmd == "edit" ) echo "\n"; echo "\n"; echo "

\n"; } function manageData_submit( $cmd ) { global $mysqlHandle, $dbname, $tablename, $fieldname, $PHP_SELF, $queryStr, $errMsg; $pResult = mysql_list_fields( $dbname, $tablename ); $num = mysql_num_fields( $pResult ); mysql_select_db( $dbname, $mysqlHandle ); if( $cmd == "add" ) $queryStr = "INSERT INTO $tablename VALUES ("; else if( $cmd == "edit" ) $queryStr = "REPLACE INTO $tablename VALUES ("; for( $i = 0; $i < $num-1; $i++ ) { $field = mysql_fetch_field( $pResult ); $func = $GLOBALS[$field->name."_function"]; if( $func != "" ) $queryStr .= " $func("; if( $field->numeric == 1 ) { $queryStr .= $GLOBALS[$field->name]; if( $func != "" ) $queryStr .= "),"; else $queryStr .= ","; } else { $queryStr .= "'" . $GLOBALS[$field->name]; if( $func != "" ) $queryStr .= "'),"; else $queryStr .= "',"; } } $field = mysql_fetch_field( $pResult ); if( $field->numeric == 1 ) $queryStr .= $GLOBALS[$field->name] . ")"; else $queryStr .= "'" . $GLOBALS[$field->name] . "')"; mysql_query( $queryStr , $mysqlHandle ); $errMsg = mysql_error(); viewData( "" ); } function deleteData() { global $mysqlHandle, $dbname, $tablename, $fieldname, $PHP_SELF, $queryStr, $errMsg; $pResult = mysql_list_fields( $dbname, $tablename ); $num = mysql_num_fields( $pResult ); $key = ""; for( $i = 0; $i < $num; $i++ ) { $field = mysql_fetch_field( $pResult, $i ); if( $field->primary_key == 1 ) if( $field->numeric == 1 ) $key .= $field->name . "=" . $GLOBALS[$field->name] . " AND "; else $key .= $field->name . "='" . $GLOBALS[$field->name] . "' AND "; } $key = substr( $key, 0, strlen($key)-4 ); mysql_select_db( $dbname, $mysqlHandle ); $queryStr = "DELETE FROM $tablename WHERE $key"; mysql_query( $queryStr, $mysqlHandle ); $errMsg = mysql_error(); viewData( "" ); } function dump() { global $PHP_SELF, $USERNAME, $PASSWORD, $action, $dbname, $tablename; if( $action == "dumpTable" ) $filename = $tablename; else $filename = $dbname; header("Content-disposition: filename=$filename.sql"); header("Content-type: application/octetstream"); header("Pragma: no-cache"); header("Expires: 0"); $pResult = mysql_query( "show variables" ); while( 1 ) { $rowArray = mysql_fetch_row( $pResult ); if( $rowArray == false ) break; if( $rowArray[0] == "basedir" ) $bindir = $rowArray[1]."bin/"; } passthru( $bindir."mysqldump --user=$USERNAME --password=$PASSWORD $dbname $tablename" ); } function utils() { global $PHP_SELF, $command; echo "

Utilities

\n"; if( $command == "" || substr( $command, 0, 5 ) == "flush" ) { echo "
\n"; echo "Show\n"; echo "\n"; echo "Flush\n"; echo "\n"; } else { $queryStr = ereg_replace( "_", " ", $command ); $pResult = mysql_query( $queryStr ); if( $pResult == false ) { echo "Fail"; return; } $col = mysql_num_fields( $pResult ); echo "

$queryStr

\n"; echo "
\n"; echo "\n"; echo "\n"; for( $i = 0; $i < $col; $i++ ) { $field = mysql_fetch_field( $pResult, $i ); echo "\n"; } echo "\n"; while( 1 ) { $rowArray = mysql_fetch_row( $pResult ); if( $rowArray == false ) break; echo "\n"; for( $j = 0; $j < $col; $j++ ) echo "\n"; echo "\n"; } echo "
".$field->name."
".htmlspecialchars( $rowArray[$j] )."
\n"; } } function header_html() { global $PHP_SELF; ?> MySQL Web Interface \n"; echo "\n"; echo "[$USERNAME] - \n"; echo "Database List | \n"; if( $tablename != "" ) echo "Table List | "; echo "Utils |\n"; echo "Logout\n"; echo "\n"; echo "\n"; echo "\n"; } //------------------------------------------------------ MAIN if( $action == "logon" || $action == "" || $action == "logout" ) logon(); else if( $action == "logon_submit" ) logon_submit(); else if( $action == "dumpTable" || $action == "dumpDB" ) { while( list($var, $value) = each($HTTP_COOKIE_VARS) ) { if( $var == "mysql_web_admin_username" ) $USERNAME = $value; if( $var == "mysql_web_admin_password" ) $PASSWORD = $value; } $mysqlHandle = mysql_pconnect( $HOSTNAME, $USERNAME, $PASSWORD ); dump(); } else { while( list($var, $value) = each($HTTP_COOKIE_VARS) ) { if( $var == "mysql_web_admin_username" ) $USERNAME = $value; if( $var == "mysql_web_admin_password" ) $PASSWORD = $value; } echo ""; if( $mysqlHandle == false ) { echo "\n"; echo "\n"; echo "MySQL Web Interface\n"; echo "\n"; echo "\n"; echo "
\n"; echo "

Wrong Password!

\n"; echo "Logon\n"; echo "
\n"; echo "\n"; echo "\n"; } else { header_html(); if( $action == "listDBs" ) listDatabases(); else if( $action == "createDB" ) createDatabase(); else if( $action == "dropDB" ) dropDatabase(); else if( $action == "listTables" ) listTables(); else if( $action == "createTable" ) createTable(); else if( $action == "dropTable" ) dropTable(); else if( $action == "viewSchema" ) viewSchema(); else if( $action == "query" ) viewData( $queryStr ); else if( $action == "addField" ) manageField( "add" ); else if( $action == "addField_submit" ) manageField_submit( "add" ); else if( $action == "editField" ) manageField( "edit" ); else if( $action == "editField_submit" ) manageField_submit( "edit" ); else if( $action == "dropField" ) dropField(); else if( $action == "viewData" ) viewData( "" ); else if( $action == "addData" ) manageData( "add" ); else if( $action == "addData_submit" ) manageData_submit( "add" ); else if( $action == "editData" ) manageData( "edit" ); else if( $action == "editData_submit" ) manageData_submit( "edit" ); else if( $action == "deleteData" ) deleteData(); else if( $action == "utils" ) utils(); mysql_close( $mysqlHandle); footer_html(); } } ?>