mysql_web 디비정보 편하게 보기, 다운로드

호스팅 시...

디비테이블 간편하게 보는 웹프로그램입니다.

간단하니...

뭐 안에 보시면 알겁니다.


프로그램 다운로드 및 소스

<?
/*
 * MySQL Web Interface Version 0.8
 * -------------------------------
 * Developed By SooMin Kim (smkim@popeye.snu.ac.kr)
 * License : GNU Public License (GPL)
 * Homepage : http://popeye.snu.ac.kr/~smkim/mysql
 */
$HOSTNAME = "localhost";
function logon() {
    global $PHP_SELF;
 setcookie( "mysql_web_admin_username" );
 setcookie( "mysql_web_admin_password" );
 echo "<html>\n";
 echo "<head>\n";
 echo "<title>MySQL 웹 매니저</title>\n";
 echo "<link rel=stylesheet href=mysql.css>\n";
 echo "</head>\n";
 echo "<body>\n";
 echo "<table width=100% height=100%><tr><td><center>\n";
        echo "<table border=1 cellpadding=0 bgcolor=#aaaaaa bordercolorlight=white bordercolordark=white>
              <tr><td><center>\n";
        echo "<table cellpadding=10><tr><td><center>\n";
 echo "<h1>MySQL 웹 매니저</h1>\n";
 echo "<form action='$PHP_SELF'>\n";
 echo "<input type=hidden name=action value=logon_submit>\n";
 echo "<table cellpadding=5 cellspacing=1>\n";
 echo "<tr><td>MySQL ID </td><td> <input type=text name=username  class=input></td></tr>\n";
 echo "<tr><td>Password </td><td> <input type=password name=password  class=input></td></tr>\n";
 echo "</table><p>\n";
 echo "<input type=submit value='확인' class=submit>\n";
 echo "<input type=reset value='지우기' class=submit><br>\n";
 echo "</form>\n";
 echo "</center></td></tr></table>\n";
 echo "</center></td></tr></table>\n";
 echo "<p><hr width=300>\n";
 echo "<font size=2>\n";
 echo "Copyleft &copy; since 1999,\n";
 echo "<a href='mailto:smkim76@icqmail.com'>SooMin Kim</a><br>\n";
 echo "<a href='http://anakii.anakii.net' target=_blank>Layout Modified by anakii 2002</a><br>";
 echo "</font>\n";
 echo "</center></td></tr></table>\n";
 echo "</body>\n";
 echo "</html>\n";
}
function logon_submit() {
    global $username, $password, $PHP_SELF;
    setcookie( "mysql_web_admin_username", $username );
    setcookie( "mysql_web_admin_password", $password );
    echo "<html>";
    echo "<head>";
    echo "<META HTTP-EQUIV=Refresh CONTENT='0; URL=$PHP_SELF?action=listDBs'>";
    echo "</head>";
    echo "</html>";
}
function echoQueryResult() {
    global $queryStr, $errMsg;
    if( $errMsg == "" ) $errMsg = "Success";
    if( $queryStr != "" ) {
        echo "<table cellpadding=5>\n";
        echo "<tr><td>Query</td><td>$queryStr</td></tr>\n";
        echo "<tr><td>Result</td><td>$errMsg</td></tr>\n";
        echo "</table><p>\n";
    }
}
function listDatabases() {
    global $mysqlHandle, $PHP_SELF;
    echo "<h1>MySQL DB 목록</h1>\n";
    echo "<form action='$PHP_SELF'>\n";
    echo "<input type=hidden name=action value=createDB>\n";
    echo "<input type=text name=dbname class=input>\n";
    echo "<input type=submit value='DB 만들기' class=submit>\n";
    echo "</form>\n";
    echo "<hr>\n";
    echo "<table cellspacing=1 cellpadding=5>\n";
    $pDB = mysql_list_dbs( $mysqlHandle );
    $num = mysql_num_rows( $pDB );
    for( $i = 0; $i < $num; $i++ ) {
        $dbname = mysql_dbname( $pDB, $i );
        echo "<tr>\n";
        echo "<td>$dbname</td>\n";
        echo "<td><a href='$PHP_SELF?action=listTables&dbname=$dbname'>테이블</a></td>\n";
        echo "<td><a href='$PHP_SELF?action=dropDB&dbname=$dbname' onClick=\"return confirm('\'$dbname\'DB를 지웁니까?')\">지우기</a></td>\n";
        echo "<td><a href='$PHP_SELF?action=dumpDB&dbname=$dbname'>백업</a></td>\n";
        echo "</tr>\n";
    }
    echo "</table>\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 "<h1>테이블 목록</h1>\n";
    echo "<p class=location>$dbname</p>\n";
    echoQueryResult();
    echo "<form action='$PHP_SELF'>\n";
    echo "<input type=hidden name=action value=createTable>\n";
    echo "<input type=hidden name=dbname value=$dbname>\n";
    echo "<input type=text name=tablename class=input>\n";
    echo "<input type=submit value='테이블 만들기' class=submit>\n";
    echo "</form>\n";
    echo "<form action='$PHP_SELF'>\n";
    echo "<input type=hidden name=action value=query>\n";
    echo "<input type=hidden name=dbname value=$dbname>\n";
    echo "<input type=text size=40 name=queryStr class=input>\n";
    //echo "<textarea cols=30 rows=3 name=queryStr></textarea><br>";
    echo "<input type=submit value='쿼리' class=submit>\n";
    echo "</form>\n";
    echo "<hr>\n";
    $pTable = mysql_list_tables( $dbname );
    if( $pTable == 0 ) {
        $msg  = mysql_error();
        echo "<h3>Error : $msg</h3><p>\n";
        return;
    }
    $num = mysql_num_rows( $pTable );
    echo "<table cellspacing=1 cellpadding=5>\n";
    for( $i = 0; $i < $num; $i++ ) {
        $tablename = mysql_tablename( $pTable, $i );
        echo "<tr>\n";
        echo "<td>\n";
        echo "$tablename\n";
        echo "</td>\n";
        echo "<td>\n";
        echo "<a href='$PHP_SELF?action=viewSchema&dbname=$dbname&tablename=$tablename'>구조</a>\n";
        echo "</td>\n";
        echo "<td>\n";
        echo "<a href='$PHP_SELF?action=viewData&dbname=$dbname&tablename=$tablename'>데이터</a>\n";
        echo "</td>\n";
        echo "<td>\n";
        echo "<a href='$PHP_SELF?action=queryData&dbname=$dbname&tablename=$tablename'>쿼리</a>\n";
        echo "</td>\n";
        echo "<td>\n";
        echo "<a href='$PHP_SELF?action=dropTable&dbname=$dbname&tablename=$tablename' onClick=\"return confirm('\'$tablename\'테이블을 지웁니까?')\">지우기</a>\n";
        echo "</td>\n";
        echo "<td>\n";
        echo "<a href='$PHP_SELF?action=dumpTable&dbname=$dbname&tablename=$tablename'>백업</a>\n";
        echo "</td>\n";
        echo "</tr>\n";
    }
    echo "</table>";
}
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 queryData() {
    global $or, $mysqlHandle, $dbname, $tablename, $PHP_SELF;
    if( $or == 0 ) $or = 1;
    echo "<h1>쿼리 내용화면</h1>\n";
    echo "<p class=location>$dbname &gt; $tablename</p>\n";
    echo "<hr>\n";
    $pResult = mysql_db_query( $dbname, "SHOW fields FROM $tablename" );
    $num = mysql_num_rows( $pResult );
    echo "<form action=$PHP_SELF>\n";
    echo "<input type=hidden name=action value=queryData_submit>\n";
    echo "<input type=hidden name=dbname value=$dbname>\n";
    echo "<input type=hidden name=tablename value=$tablename>\n";
    echo "<input type=hidden name=or value=$or>\n";
    echo "<table cellspacing=1 cellpadding=5>\n";
    echo "<tr>\n";
    echo "<th>데이터 필드(Field)</th>\n";
    echo "<th>데이터 형식(Type)</th>\n";
    echo "<th>보이기</th>\n";
    echo "<th>정렬선택</th>\n";
    for( $j = 0; $j < $or; $j++ )
        echo "<th>검색내용</th>\n";
    echo "<td rowspan=".($num+1).">";
    echo "<a href='$PHP_SELF?action=queryData&dbname=$dbname&tablename=$tablename&or=".($or+1)."'>Or</a>\n";
    echo "</td>\n";
    echo "</tr>\n";
    for( $i = 0; $i < $num; $i++ ) {
        $field = mysql_fetch_array( $pResult );
        $fieldname = $field["Field"];
        echo "<tr>\n";
        echo "<td>$fieldname</td>\n";
        echo "<td>".$field["Type"]."</td>\n";
        echo "<td align=center><input type=checkbox name=select_$fieldname value=ok checked></td>\n";
        echo "<td align=center><input type=radio name=orderby value=$fieldname></td>\n";
        for( $j = 0; $j < $or; $j++ ) {
            echo "<td>\n";
            echo "<select name=op_${fieldname}>\n";
            echo "<option value='='>=\n";
            echo "<option value='<'>&lt;\n";
            echo "<option value='>'>&gt;\n";
            echo "<option value=' LIKE '>LIKE\n";
            echo "</select>\n";
            echo "<input type=text name=criteria1_$fieldname class=input>\n";
            echo "</td>\n";
        }
        echo "</tr>\n";
    }
    echo "</table>\n";
    echo "<input type=submit value='쿼리' class=submit>\n";
    echo "</form>\n";
}
function queryData_submit() {
    global $mysqlHandle, $dbname, $tablename, $orderby, $PHP_SELF;
    $pResult = mysql_db_query( $dbname, "SHOW fields FROM $tablename" );
    $num = mysql_num_rows( $pResult );
    $select = "";
    $where = "";
    for( $i = 0; $i < $num; $i++ ) {
        $field = mysql_fetch_array( $pResult );
        $fieldname = $field["Field"];
        if( $GLOBALS["select_$fieldname"] == "ok" )
            $select .= "$fieldname, ";
        if( $GLOBALS["criteria1_$fieldname"] != "" ) {
            $where .= $fieldname.$GLOBALS["op_$fieldname"]."'".$GLOBALS["criteria1_$fieldname"]."' AND ";
        }
    }
    if( $select != "" ) {
        $select = substr( $select, 0, strlen($select)-2 );
    }
    $queryStr = "SELECT $select FROM $tablename";
    if( $where != "" ) {
        $where = substr( $where, 0, strlen($where)-5 );
        $queryStr .= " WHERE $where";
    }
    if( $orderby != "" ) $queryStr .= " ORDER BY $orderby";
    viewData( $queryStr );
}
function viewSchema() {
    global $mysqlHandle, $dbname, $tablename, $PHP_SELF, $queryStr, $errMsg;
    echo "<h1>테이블 구조</h1>\n";
    echo "<p class=location>$dbname &gt; $tablename</p>\n";
    echoQueryResult();
    echo "<a href='$PHP_SELF?action=addField&dbname=$dbname&tablename=$tablename'>필드 추가</a> | \n";
    echo "<a href='$PHP_SELF?action=viewData&dbname=$dbname&tablename=$tablename'>데이터 보기</a>\n";
    echo "<hr>\n";
    $pResult = mysql_db_query( $dbname, "SHOW fields FROM $tablename" );
    $num = mysql_num_rows( $pResult );
    echo "<table cellspacing=1 cellpadding=5>\n";
    echo "<tr>\n";
    echo "<th>Field</th>\n";
    echo "<th>Type</th>\n";
    echo "<th>Null</th>\n";
    echo "<th>Key</th>\n";
    echo "<th>Default</th>\n";
    echo "<th>Extra</th>\n";
    echo "<th colspan=2>Action</th>\n";
    echo "</tr>\n";
    for( $i = 0; $i < $num; $i++ ) {
        $field = mysql_fetch_array( $pResult );
        echo "<tr>\n";
        echo "<td>".$field["Field"]."</td>\n";
        echo "<td>".$field["Type"]."</td>\n";
        echo "<td>".$field["Null"]."</td>\n";
        echo "<td>".$field["Key"]."</td>\n";
        echo "<td>".$field["Default"]."</td>\n";
        echo "<td>".$field["Extra"]."</td>\n";
        $fieldname = $field["Field"];
        echo "<td><a href='$PHP_SELF?action=editField&dbname=$dbname&tablename=$tablename&fieldname=$fieldname'>수정</a></td>\n";
        echo "<td><a href='$PHP_SELF?action=dropField&dbname=$dbname&tablename=$tablename&fieldname=$fieldname' onClick=\"return confirm('\'$fieldname\'필드를 지웁니까?')\">삭제</a></td>\n";
        echo "</tr>\n";
    }
    echo "</table>\n";
}
function manageField( $cmd ) {
    global $mysqlHandle, $dbname, $tablename, $fieldname, $PHP_SELF;
    if( $cmd == "add" )
        echo "<h1>필드 추가</h1>\n";
    else if( $cmd == "edit" ) {
        echo "<h1>필드 수정</h1>\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 "<p class=location>$dbname &gt; $tablename</p>\n";
    echo "<form action=$PHP_SELF>\n";
    if( $cmd == "add" )
        echo "<input type=hidden name=action value=addField_submit>\n";
    else if( $cmd == "edit" ) {
        echo "<input type=hidden name=action value=editField_submit>\n";
        echo "<input type=hidden name=old_name value=$fieldname>\n";
    }
    echo "<input type=hidden name=dbname value=$dbname>\n";
    echo "<input type=hidden name=tablename value=$tablename>\n";
    echo "<h3>필드이름</h3>\n";
    echo "<input type=text class=input name=name value=$fieldname><p>\n";
?>
<h3>필드형식</h3>
<font size=2>
* `M' 은 최대로 보여질 수 있는 크기를 말합니다.<br>
* `D' 는 실수형식에 적용되며 소수점 아래 자리를 가리킵니다.<br>
</font>
<table>
<tr>
<th>Type</th><th>&nbspM&nbsp</th><th>&nbspD&nbsp</th><th>unsigned</th><th>zerofill</th><th>binary</th>
</tr>
<tr>
<td><input type=radio name=type value="TINYINT" <? if( $type == "tinyint" ) echo "checked";?>>TINYINT (-128 ~ 127)</td>
<td align=center>O</td>
<td>&nbsp</td>
<td align=center>O</td>
<td align=center>O</td>
<td>&nbsp</td>
</tr>
<tr>
<td><input type=radio name=type value="SMALLINT" <? if( $type == "smallint" ) echo "checked";?>>SMALLINT (-32768 ~ 32767)</td>
<td align=center>O</td>
<td>&nbsp</td>
<td align=center>O</td>
<td align=center>O</td>
<td>&nbsp</td>
</tr>
<tr>
<td><input type=radio name=type value="MEDIUMINT" <? if( $type == "mediumint" ) echo "checked";?>>MEDIUMINT (-8388608 ~ 8388607)</td>
<td align=center>O</td>
<td>&nbsp</td>
<td align=center>O</td>
<td align=center>O</td>
<td>&nbsp</td>
</tr>
<tr>
<td><input type=radio name=type value="INT" <? if( $type == "int" ) echo "checked";?>>INT (-2147483648 ~ 2147483647)</td>
<td align=center>O</td>
<td>&nbsp</td>
<td align=center>O</td>
<td align=center>O</td>
<td>&nbsp</td>
</tr>
<tr>
<td><input type=radio name=type value="BIGINT" <? if( $type == "bigint" ) echo "checked";?>>BIGINT (-9223372036854775808 ~ 9223372036854775807)</td>
<td align=center>O</td>
<td>&nbsp</td>
<td align=center>O</td>
<td align=center>O</td>
<td>&nbsp</td>
</tr>
<tr>
<td><input type=radio name=type value="FLOAT" <? if( $type == "float" ) echo "checked";?>>FLOAT</td>
<td align=center>O</td>
<td align=center>O</td>
<td>&nbsp</td>
<td align=center>O</td>
<td>&nbsp</td>
</tr>
<tr>
<td><input type=radio name=type value="DOUBLE" <? if( $type == "double" ) echo "checked";?>>DOUBLE</td>
<td align=center>O</td>
<td align=center>O</td>
<td>&nbsp</td>
<td align=center>O</td>
<td>&nbsp</td>
</tr>
<tr>
<td><input type=radio name=type value="DECIMAL" <? if( $type == "decimal" ) echo "checked";?>>DECIMAL(NUMERIC)</td>
<td align=center>O</td>
<td align=center>O</td>
<td>&nbsp</td>
<td align=center>O</td>
<td>&nbsp</td>
</tr>
<tr>
<td><input type=radio name=type value="DATE" <? if( $type == "date" ) echo "checked";?>>DATE (1000-01-01 ~ 9999-12-31, YYYY-MM-DD)</td>
<td>&nbsp</td>
<td>&nbsp</td>
<td>&nbsp</td>
<td>&nbsp</td>
<td>&nbsp</td>
</tr>
<tr>
<td><input type=radio name=type value="DATETIME" <? if( $type == "datetime" ) echo "checked";?>>DATETIME (1000-01-01 00:00:00 ~ 9999-12-31 23:59:59, YYYY-MM-DD HH:MM:SS)</td>
<td>&nbsp</td>
<td>&nbsp</td>
<td>&nbsp</td>
<td>&nbsp</td>
<td>&nbsp</td>
</tr>
<tr>
<td><input type=radio name=type value="TIMESTAMP" <? if( $type == "timestamp" ) echo "checked";?>>TIMESTAMP (1970-01-01 00:00:00 ~ 2106..., YYYYMMDD[HH[MM[SS]]])</td>
<td align=center>O</td>
<td>&nbsp</td>
<td>&nbsp</td>
<td>&nbsp</td>
<td>&nbsp</td>
</tr>
<tr>
<td><input type=radio name=type value="TIME" <? if( $type == "time" ) echo "checked";?>>TIME (-838:59:59 ~ 838:59:59, HH:MM:SS)</td>
<td>&nbsp</td>
<td>&nbsp</td>
<td>&nbsp</td>
<td>&nbsp</td>
<td>&nbsp</td>
</tr>
<tr>
<td><input type=radio name=type value="YEAR" <? if( $type == "year" ) echo "checked";?>>YEAR (1901 ~ 2155, 0000, YYYY)</td>
<td>&nbsp</td>
<td>&nbsp</td>
<td>&nbsp</td>
<td>&nbsp</td>
<td>&nbsp</td>
</tr>
<tr>
<td><input type=radio name=type value="CHAR" <? if( $type == "char" ) echo "checked";?>>CHAR</td>
<td align=center>O</td>
<td>&nbsp</td>
<td>&nbsp</td>
<td>&nbsp</td>
<td align=center>O</td>
</tr>
<tr>
<td><input type=radio name=type value="VARCHAR" <? if( $type == "varchar" ) echo "checked";?>>VARCHAR</td>
<td align=center>O</td>
<td>&nbsp</td>
<td>&nbsp</td>
<td>&nbsp</td>
<td align=center>O</td>
</tr>
<tr>
<td><input type=radio name=type value="TINYTEXT" <? if( $type == "tinytext" ) echo "checked";?>>TINYTEXT (0 ~ 255)</td>
<td>&nbsp</td>
<td>&nbsp</td>
<td>&nbsp</td>
<td>&nbsp</td>
<td>&nbsp</td>
</tr>
<tr>
<td><input type=radio name=type value="TEXT" <? if( $type == "text" ) echo "checked";?>>TEXT (0 ~ 65535)</td>
<td>&nbsp</td>
<td>&nbsp</td>
<td>&nbsp</td>
<td>&nbsp</td>
<td>&nbsp</td>
</tr>
<tr>
<td><input type=radio name=type value="MEDIUMTEXT" <? if( $type == "mediumtext" ) echo "checked";?>>MEDIUMTEXT (0 ~ 16777215)</td>
<td>&nbsp</td>
<td>&nbsp</td>
<td>&nbsp</td>
<td>&nbsp</td>
<td>&nbsp</td>
</tr>
<tr>
<td><input type=radio name=type value="LONGTEXT" <? if( $type == "longtext" ) echo "checked";?>>LONGTEXT (0 ~ 4294967295)</td>
<td>&nbsp</td>
<td>&nbsp</td>
<td>&nbsp</td>
<td>&nbsp</td>
<td>&nbsp</td>
</tr>
<tr>
<td><input type=radio name=type value="TINYBLOB" <? if( $type == "tinyblob" ) echo "checked";?>>TINYBLOB (0 ~ 255)</td>
<td>&nbsp</td>
<td>&nbsp</td>
<td>&nbsp</td>
<td>&nbsp</td>
<td>&nbsp</td>
</tr>
<tr>
<td><input type=radio name=type value="BLOB" <? if( $type == "blob" ) echo "checked";?>>BLOB (0 ~ 65535)</td>
<td>&nbsp</td>
<td>&nbsp</td>
<td>&nbsp</td>
<td>&nbsp</td>
<td>&nbsp</td>
</tr>
<tr>
<td><input type=radio name=type value="MEDIUMBLOB" <? if( $type == "mediumblob" ) echo "checked";?>>MEDIUMBLOB (0 ~ 16777215)</td>
<td>&nbsp</td>
<td>&nbsp</td>
<td>&nbsp</td>
<td>&nbsp</td>
<td>&nbsp</td>
</tr>
<tr>
<td><input type=radio name=type value="LONGBLOB" <? if( $type == "longblob" ) echo "checked";?>>LONGBLOB (0 ~ 4294967295)</td>
<td>&nbsp</td>
<td>&nbsp</td>
<td>&nbsp</td>
<td>&nbsp</td>
<td>&nbsp</td>
</tr>
<tr>
<td><input type=radio name=type value="ENUM" <? if( $type == "enum" ) echo "checked";?>>ENUM</td>
<td colspan=5><center>value list</center></td>
</tr>
<tr>
<td><input type=radio name=type value="SET" <? if( $type == "set" ) echo "checked";?>>SET</td>
<td colspan=5><center>value list</center></td>
</tr>
</table>
<table>
<tr><th>M</th><th>D</th><th>unsigned</th><th>zerofill</th><th>binary</th><th>value list (ex: 'apple', 'orange', 'banana') </th></tr>
<tr>
<td align=center><input type=text class=input size=4 name=M <? if( $M != "" ) echo "value=$M";?>></td>
<td align=center><input type=text class=input size=4 name=D <? if( $D != "" ) echo "value=$D";?>></td>
<td align=center><input type=checkbox name=unsigned value="UNSIGNED" <? if( strpos( $fieldtype, "unsigned" ) ) echo "checked";?>></td>
<td align=center><input type=checkbox name=zerofill value="ZEROFILL" <? if( strpos( $fieldtype, "zerofill" ) ) echo "checked";?>></td>
<td align=center><input type=checkbox name=binary value="BINARY" <? if( strpos( $fieldtype, "binary" )  ) echo "checked";?>></td>
<td align=center><input type=text class=input size=60 name=valuelist <? if( $valuelist != "" ) echo "value=\"$valuelist\"";?>></td>
</tr>
</table>

<h3>Flags</h3>
<table>
<tr><th>not null</th><th>default value</th><th>auto increment</th><th>primary key</th></tr>
<tr>
<td align=center><input type=checkbox name=not_null value="NOT NULL" <? if( $fieldnull != "YES" ) echo "checked";?>></td>
<td align=center><input type=text class=input name=default_value <? if( $fielddefault != "" ) echo "value=$fielddefault";?>></td>
<td align=center><input type=checkbox name=auto_increment value="AUTO_INCREMENT" <? if( $fieldextra == "auto_increment" ) echo "checked";?>></td>
<td align=center><input type=checkbox name=primary_key value="PRIMARY KEY" <? if( $fieldkey == "PRI" ) echo "checked";?>></td>
</tr>
</table>
<p>
<?
    if( $cmd == "add" )
        echo "<input type=submit value='필드 추가' class=submit>\n";
    else if( $cmd == "edit" )
        echo "<input type=submit value='필드 수정' class=submit>\n";
    echo "<input type=button class=submit value=취소 onClick='history.back()'>\n";
    echo "</form>\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 .= "<br>\n" . $q;
        $errMsg .= "<br>\n" . mysql_error();
        $q = "ALTER TABLE $tablename ADD PRIMARY KEY( $primary )";
        mysql_query( $q );
        $queryStr .= "<br>\n" . $q;
        $errMsg .= "<br>\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 "<h1>테이블 안의 데이터 보기</h1>\n";
    if( $tablename != "" )
        echo "<p class=location>$dbname &gt; $tablename</p>\n";
    else
        echo "<p class=location>$dbname</p>\n";
    $queryStr = stripslashes( $queryStr );
    if( $queryStr == "" ) {
        $queryStr = "SELECT * FROM $tablename";
        if( $orderby != "" )
            $queryStr .= " ORDER BY $orderby";
        echo "<a href='$PHP_SELF?action=addData&dbname=$dbname&tablename=$tablename'>데이터 추가</a> | \n";
        echo "<a href='$PHP_SELF?action=viewSchema&dbname=$dbname&tablename=$tablename'>구조</a>\n";
    }
    $pResult = mysql_db_query( $dbname, $queryStr );
    $errMsg = mysql_error();
    $GLOBALS[queryStr] = $queryStr;
    if( $pResult == false ) {
        echoQueryResult();
        return;
    }
    if( $pResult == 1 ) {
        $errMsg = "성공했습니다";
        echoQueryResult();
        return;
    }
    echo "<hr>\n";
    $row = mysql_num_rows( $pResult );
    $col = mysql_num_fields( $pResult );
    if( $row == 0 ) {
        echo "아무 데이터가 없습니다!";
        return;
    }
    if( $rowperpage == "" ) $rowperpage = 20;
    if( $page == "" ) $page = 0;
    else $page--;
    mysql_data_seek( $pResult, $page * $rowperpage );
    echo "<table cellspacing=1 cellpadding=2>\n";
    echo "<tr>\n";
    for( $i = 0; $i < $col; $i++ ) {
        $field = mysql_fetch_field( $pResult, $i );
        echo "<th>";
        echo "<a href='$PHP_SELF?action=viewData&dbname=$dbname&tablename=$tablename&orderby=".$field->name."'>".$field->name."</a>\n";
        echo "</th>\n";
    }
    echo "<th colspan=2>Action</th>\n";
    echo "</tr>\n";
    for( $i = 0; $i < $rowperpage; $i++ ) {
        $rowArray = mysql_fetch_row( $pResult );
        if( $rowArray == false ) break;
        echo "<tr>\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 "<td>\n";
            echo "$data\n";
            echo "</td>\n";
        }
        if( $key == "" )
            echo "<td colspan=2>no Key</td>\n";
        else {
            echo "<td><a href='$PHP_SELF?action=editData&dbname=$dbname&tablename=$tablename$key'>수정</a></td>\n";
            echo "<td><a href='$PHP_SELF?action=deleteData&dbname=$dbname&tablename=$tablename$key' onClick=\"return confirm('이 줄을 지울까요?')\">삭제</a></td>\n";
        }
        echo "</tr>\n";
    }
    echo "</table>\n";
    echo "<font size=2>\n";
    echo "<form action='$PHP_SELF?action=viewData&dbname=$dbname&tablename=$tablename' method=post>\n";
    echo "<font color=green>\n";
    echo ($page+1)."/".(int)($row/$rowperpage+1)." 페이지";
    echo "</font>\n";
    echo " | ";
    if( $page > 0 ) {
        echo "<a href='$PHP_SELF?action=viewData&dbname=$dbname&tablename=$tablename&page=".($page);
        if( $orderby != "" )
            echo "&orderby=$orderby";
        echo "'>이전</a>\n";
    } else
        echo "이전";
    echo " | ";
    if( $page < ($row/$rowperpage)-1 ) {
        echo "<a href='$PHP_SELF?action=viewData&dbname=$dbname&tablename=$tablename&page=".($page+2);
        if( $orderby != "" )
            echo "&orderby=$orderby";
        echo "'>다음</a>\n";
    } else
        echo "다음";
    echo " | ";
    if( $row > $rowperpage ) {
        echo "<input type=text size=4 name=page class=input>\n";
        echo "<input type=submit value='이동' class=submit>\n";
    }
    echo "</form>\n";
    echo "</font>\n";
}
function manageData( $cmd ) {
    global $mysqlHandle, $dbname, $tablename, $PHP_SELF;
    if( $cmd == "add" )
        echo "<h1>데이터 추가</h1>\n";
    else if( $cmd == "edit" ) {
        echo "<h1>데이터 수정</h1>\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 "<p class=location>$dbname &gt; $tablename</p>\n";
    echo "<form action='$PHP_SELF' method=post>\n";
    if( $cmd == "add" )
        echo "<input type=hidden name=action value=addData_submit>\n";
    else if( $cmd == "edit" )
        echo "<input type=hidden name=action value=editData_submit>\n";
    echo "<input type=hidden name=dbname value=$dbname>\n";
    echo "<input type=hidden name=tablename value=$tablename>\n";
    echo "<table cellspacing=1 cellpadding=2>\n";
    echo "<tr>\n";
    echo "<th>Name</th>\n";
    echo "<th>Type</th>\n";
    echo "<th>Function</th>\n";
    echo "<th>Data</th>\n";
    echo "</tr>\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 "<tr>";
        echo "<td>$fieldname</td>";
        echo "<td>".$field["Type"]."</td>";
        echo "<td>\n";
        echo "<select name=${fieldname}_function>\n";
        echo "<option>\n";
        echo "<option>ASCII\n";
        echo "<option>CHAR\n";
        echo "<option>SOUNDEX\n";
        echo "<option>CURDATE\n";
        echo "<option>CURTIME\n";
        echo "<option>FROM_DAYS\n";
        echo "<option>FROM_UNIXTIME\n";
        echo "<option>NOW\n";
        echo "<option>PASSWORD\n";
        echo "<option>PERIOD_ADD\n";
        echo "<option>PERIOD_DIFF\n";
        echo "<option>TO_DAYS\n";
        echo "<option>USER\n";
        echo "<option>WEEKDAY\n";
        echo "<option>RAND\n";
        echo "</select>\n";
        echo "</td>\n";
        $value = htmlspecialchars($data[$i]);
        if( $cmd == "add" ) {
            $type = strtok( $fieldtype, " (,)\n" );
            if( $type == "enum" || $type == "set" ) {
                echo "<td>\n";
                if( $type == "enum" )
                    echo "<select name='$fieldname'>\n";
                else if( $type == "set" )
                    echo "<select name='$fieldname' size=4 multiple>\n";
                echo strtok( "'" );
                while( $str = strtok( "'" ) ) {
                    echo "<option>$str\n";
                    strtok( "'" );
                }
                echo "</select>\n";
                echo "</td>\n";
            } else {
                if( $len < 40 )
                    echo "<td><input type=text class=input size=40 maxlength=$len name='$fieldname'></td>\n";
                else
                    echo "<td><textarea cols=40 rows=3 maxlength=$len name='$fieldname'></textarea>\n";
            }
        } else if( $cmd == "edit" ) {
            $type = strtok( $fieldtype, " (,)\n" );
            if( $type == "enum" || $type == "set" ) {
                echo "<td>\n";
                if( $type == "enum" )
                    echo "<select name='$fieldname'>\n";
                else if( $type == "set" )
                    echo "<select name='$fieldname' size=4 multiple>\n";
                echo strtok( "'" );
                while( $str = strtok( "'" ) ) {
                    if( $value == $str )
                        echo "<option selected>$str\n";
                    else
                        echo "<option>$str\n";
                    strtok( "'" );
                }
                echo "</select>\n";
                echo "</td>\n";
            } else {
                if( $len < 40 )
                    echo "<td><input type=text class=input size=40 maxlength=$len name='$fieldname' value=\"$value\"></td>\n";
                else
                    echo "<td><textarea cols=40 rows=3 maxlength=$len name='$fieldname'>$value</textarea>\n";
            }
        }
        echo "</tr>";
    }
    echo "</table><p>\n";
    if( $cmd == "add" )
        echo "<input type=submit value='데이터 추가' class=submit>\n";
    else if( $cmd == "edit" )
        echo "<input type=submit value='데이터 수정' class=submit>\n";
    echo "<input type=button class=submit value='취소' onClick='history.back()'>\n";
    echo "</form>\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 "<h1>유틸리티</h1>\n";
    if( $command == "" || substr( $command, 0, 5 ) == "flush" ) {
        echo "<hr>\n";
        echo "Show\n";
        echo "<ul>\n";
        echo "<li><a href='$PHP_SELF?action=utils&command=show_status'>상태(Status)</a>\n";
        echo "<li><a href='$PHP_SELF?action=utils&command=show_variables'>변수(Variables)</a>\n";
        echo "<li><a href='$PHP_SELF?action=utils&command=show_processlist'>작업목록(Processlist)</a>\n";
        echo "</ul>\n";
        echo "Flush\n";
        echo "<ul>\n";
        echo "<li><a href='$PHP_SELF?action=utils&command=flush_hosts'>호스트</a>\n";
        if( $command == "flush_hosts" ) {
            if( mysql_query( "Flush hosts" ) != false )
                echo "<font size=2 color=red>- 성공</font>";
            else
                echo "<font size=2 color=red>- 실패</font>";
        }
        echo "<li><a href='$PHP_SELF?action=utils&command=flush_logs'>로그</a>\n";
        if( $command == "flush_logs" ) {
            if( mysql_query( "Flush logs" ) != false )
                echo "<font size=2 color=red>- 성공</font>";
            else
                echo "<font size=2 color=red>- 실패</font>";
        }
        echo "<li><a href='$PHP_SELF?action=utils&command=flush_privileges'>권한</a>\n";
        if( $command == "flush_privileges" ) {
            if( mysql_query( "Flush privileges" ) != false )
                echo "<font size=2 color=red>- 성공</font>";
            else
                echo "<font size=2 color=red>- 실패</font>";
        }
        echo "<li><a href='$PHP_SELF?action=utils&command=flush_tables'>테이블</a>\n";
        if( $command == "flush_tables" ) {
            if( mysql_query( "Flush tables" ) != false )
                echo "<font size=2 color=red>- 성공</font>";
            else
                echo "<font size=2 color=red>- 실패</font>";
        }
        echo "<li><a href='$PHP_SELF?action=utils&command=flush_status'>상태</a>\n";
        if( $command == "flush_status" ) {
            if( mysql_query( "Flush status" ) != false )
                echo "<font size=2 color=red>- 성공</font>";
            else
                echo "<font size=2 color=red>- 실패</font>";
        }
        echo "</ul>\n";
    } else {
        $queryStr = ereg_replace( "_", " ", $command );
        $pResult = mysql_query( $queryStr );
        if( $pResult == false ) {
            echo "실패";
            return;
        }
        $col = mysql_num_fields( $pResult );
        echo "<p class=location>$queryStr</p>\n";
        echo "<hr>\n";
        echo "<table cellspacing=1 cellpadding=2 border=0>\n";
        echo "<tr>\n";
        for( $i = 0; $i < $col; $i++ ) {
            $field = mysql_fetch_field( $pResult, $i );
            echo "<th>".$field->name."</th>\n";
        }
        echo "</tr>\n";
        while( 1 ) {
            $rowArray = mysql_fetch_row( $pResult );
            if( $rowArray == false ) break;
            echo "<tr>\n";
            for( $j = 0; $j < $col; $j++ )
                echo "<td>".htmlspecialchars( $rowArray[$j] )."</td>\n";
            echo "</tr>\n";
        }
        echo "</table>\n";
    }
}
function header_html() {
    global $PHP_SELF;
?>
<html>
<head>
<title>MySQL 웹 편집기</title>
<link rel=stylesheet href=mysql.css>
</head>
<body>
<?
}
function footer_html() {
    global $mysqlHandle, $dbname, $tablename, $PHP_SELF, $USERNAME;
    echo "<hr>\n";
    echo "<font size=2>\n";
    echo "<font color=blue>[$USERNAME]</font> - \n";
    echo "<a href='$PHP_SELF?action=listDBs'>DB 목록</a> | \n";
    if( $tablename != "" )
        echo "<a href='$PHP_SELF?action=listTables&dbname=$dbname&tablename=$tablename'>테이블 목록</a> | ";
    echo "<a href='$PHP_SELF?action=utils'>유틸리티</a> |\n";
    echo "<a href='$PHP_SELF?action=logout'>로그아웃</a>\n";
    echo "</font>\n";
    echo "</body>\n";
    echo "</html>\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 "<!--";
    $mysqlHandle = mysql_pconnect( $HOSTNAME, $USERNAME, $PASSWORD );
    echo "-->";
    if( $mysqlHandle == false ) {
        echo "<html>\n";
        echo "<head>\n";
        echo "<title>MySQL 웹 툴</title>\n";
        echo "<link rel=stylesheet href=mysql.css>\n";
        echo "</head>\n";
        echo "<body>\n";
        echo "<table width=100% height=100%><tr><td><center>\n";
        echo "<h1>비밀번호가 틀립니다!</h1>\n";
        echo "<a href='$PHP_SELF?action=logon'>접속</a>\n";
        echo "</center></td></tr></table>\n";
        echo "</body>\n";
        echo "</html>\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 == "queryData" )
            queryData();
        else if( $action == "queryData_submit" )
            queryData_submit();
        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();
    }
}
?>