Skip to content

Instantly share code, notes, and snippets.

@autioch
Created November 7, 2017 19:20
Show Gist options
  • Save autioch/c5165f223a1bc6a2936a68565059c149 to your computer and use it in GitHub Desktop.
Save autioch/c5165f223a1bc6a2936a68565059c149 to your computer and use it in GitHub Desktop.
One file database admin
<!doctype html>
<html>
<head>
<title>Database Administration</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-2">
<style>
body {width:96%; margin:auto; margin-top:10px; border:auto; padding:auto; font-size:13px; color:#005000; text-align:center;font-family:Helvetica,Arial;background-color:#ffffff;}
a {font-weight:bold; text-decoration:none; color:#005000; }
a.hover {font-weight:bold; text-decoration:underline; color:#005000; }
a.active {font-weight:bold; text-decoration:none; color:#005000; }
a.visited {font-weight:bold; text-decoration:none; color:#005000; }
.form {-moz-border-radius: 5px; border-style: solid; background-color:#99F099; border-color: #32CD32; }
.field {width: 150px; text-align: left; }
.error {margin: 10px; padding: 3px; font-weight: bold; color: #ff0000; }
.location {border: 2px; padding: 4px; -moz-border-radius: 5px; border-style: solid; background-color:#99F099; border-color: #32CD32; text-align:left;}
.col {border: 1px; padding: 2px; background-color:#CAFCC9; border-color: #32CD32;}
.tab {border: 2px; padding: 0px; -moz-border-radius: 3px; border-style: solid; background-color:#99F099; border-color: #32CD32; }
.info {width: 750px; padding: 4px; margin: auto; border: auto; -moz-border-radius: 5px; border-style: normal; background-color:#CAFCC9; }
.opt {padding: 12px; text-align: left; margin: auto; line-height: 20px; border: auto; -moz-border-radius: 5px; border-style: normal; background-color:#CAFCC9; }
</style>
</head>
<body>
<?php
error_reporting(E_ALL);
function con($host, $login, $password)
{
$e = '';
if (mb_strlen($host) < 1) {
$e = '<br>No server specified.';
}
if (mb_strlen($login) < 1) {
$e = $e . '<br>No login entered.';
}
if (mb_strlen($password) < 1) {
$e = $e . '<br>No password entered.';
}
if (!@mysql_pconnect($host, $login, $password)) {
global $error;
$error = $error . $e . '<br>Connection to the database server failed.';
return false;
}
return true;
}
function q($query)
{
$result = @mysql_query($query);
if ($result) {
return $result;
} else {
global $error;
$error = $error . '<br>Error: ' . $query . ' ( ' . mysql_error() . ' )';
return false;
}
}
function db()
{
$result = q('SHOW DATABASES;');
echo '<select onchange="self.location.href = this.value"><option value="index.php">Choose database</option>';
while ($rekord = mysql_fetch_assoc($result)) {
echo '<option value="index.php?d=' . $rekord['Database'] . '">' . $rekord['Database'] . '</option>';
}
echo '</select>';
mysql_free_result($result);
}
function t($db)
{
mysql_select_db($db);
$result = q('SHOW TABLES;');
if (mysql_num_rows($result) > 0) {
echo '<select onchange="self.location.href = this.value"><option value="index.php">Choose table</option>';
while ($rekord = mysql_fetch_assoc($result)) {
echo '<option value="index.php?d=' . $db . '&t=' . $rekord['Tables_in_' . $db] . '">' . $rekord['Tables_in_' . $db] . '</option>';
}
echo '</select>';
}
mysql_free_result($result);
}
function t_view($table)
{
$off = isset($_GET['off']) ? $_GET['off'] : 0;
$result = q('SELECT COUNT(*) FROM ' . $table . ';');
$i = mysql_fetch_assoc($result);
$il = $i['COUNT(*)'];
$result = q('SELECT * FROM ' . $table . ' LIMIT ' . $off . ',20;');
$result = q('DESCRIBE ' . $table . ';');
echo '<br><table class="tab" align="center">
<tr style="background-color: #CAFCC9;">
<td width="150">Field</td><td width="200">Type</td><td width="50">Null</td><td width="60">Key</td><td width="100">Default</td><td width="130">Extra</td>
</tr>';
$kol = [];
$a = 1;
while ($r = mysql_fetch_assoc($result)) {
echo '<tr', 0 == ($a % 2) ? ' style="background-color: #CAFCC9;"' : '', '>';
echo '<td>' ,$r['Field'] , '</td><td>' , $r['Type'] , '</td><td>' , $r['Null'] , '</td><td>' , $r['Key'] , '</td><td>' , $r['Default'] , '</td><td>' , $r['Extra'] , '</td>';
echo '</tr>';
$kol[] = $r['Field'];
++$a;
}
if (isset($_POST['adding'])) {
$values = implode("','", $_POST['adding']);
q('INSERT INTO ' . $_GET['t'] . " VALUES ('" . $values . "');");
}
if (isset($_POST['index'])) {
$index = $_POST['index'];
q('DELETE FROM ' . $_GET['t'] . ' WHERE ' . $kol[0] . "='" . $index . "';");
}
echo '</table>';
$off = isset($_GET['off']) ? $_GET['off'] : 0;
$result = q('SELECT * FROM ' . $table . ' LIMIT ' . $off . ',20;');
if ($il > 0) {
echo '<br><div class="info">';
if (($off + 20) < $il) {
echo '<div style="float: right;"><a href="index.php?d=' . $_GET['d'] . '&t=' . $_GET['t'] . '&off=' . ($off + 20) . '">20 rows --></a></div>';
}
if ($off > 19) {
echo '<div style="float: left;"><a href="index.php?d=' . $_GET['d'] . '&t=' . $_GET['t'] . '&off=' . ($off - 20) . '"><-- 20 rows</a></div>';
}
echo $il . ' rows, offset ' . $off . '</div>';
}
echo '<br><table align="center" class="tab">
<tr style="background-color: #CAFCC9;">';
foreach ($kol as $p) {
echo '<td>' . $p . '</td>';
}
echo '<td>Option</td></tr>
<tr><form action="index.php?d=' . $_GET['d'] . '&t=' . $_GET['t'] . '&off=' . $off . '" method=post>';
foreach ($kol as $p) {
echo '<td><input name="adding[]" size="' . (mb_strlen($p) - 1) . '" type="text"></td>';
}
echo '<td><input value="Add" type=submit></td></form></tr>';
$a = 0;
while ($rek = mysql_fetch_assoc($result)) {
echo '<tr', 0 == ($a % 2) ? ' style="background-color: #CAFCC9;"' : '', '>';
foreach ($kol as $p) {
echo '<td>' . $rek[$p] . '</td>';
}
echo '<td>
<form action="index.php?d=' . $_GET['d'] . '&t=' . $_GET['t'] . '&off=' . $off . '" method=post>
<input value="Drop" type=submit>
<input name="index" value="' . $rek[$kol[0]] . '" type=hidden>
</form>
</td></tr>';
++$a;
}
echo '</table>';
mysql_free_result($result);
}
function db_opt()
{
if (isset($_POST['new_t']) && isset($_POST['columns'])) {
if (empty($_POST['options'])) {
q('CREATE TABLE ' . $_POST['new_t'] . ' ( ' . $_POST['columns'] . '); ');
} else {
q('CREATE TABLE ' . $_POST['new_t'] . ' ( ' . $_POST['columns'] . ') ' . $_POST['options'] . '; ');
}
}
if (isset($_POST['drop_t'])) {
q('DROP TABLE ' . $_POST['drop_t']);
}
if (isset($_POST['alter_t'])) {
q($_POST['alter_t']);
}
echo '<br>
<div class="opt">
Create table
<br>
CREATE TABLE creates a table with the given name. the CREATE privilege for the table is required.
<br><br>
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name (create_definition,...) [table_option ...]
<br>
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name [(create_definition,...)] [table_option ...] select_statement
<br>
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name { LIKE old_tbl_name | (LIKE old_tbl_name) }
<br><br>
create_definition
<ul>
<li>col_name column_definition
<li>[CONSTRAINT [symbol]] PRIMARY KEY [index_type] (index_col_name,...)
<li>{INDEX|KEY} [index_name] [index_type] (index_col_name,...)
<li>[CONSTRAINT [symbol]] UNIQUE [INDEX|KEY] [index_name] [index_type] (index_col_name,...)
<li>{FULLTEXT|SPATIAL} [INDEX|KEY] [index_name] (index_col_name,...)
<li>[CONSTRAINT [symbol]] FOREIGN KEY [index_name] (index_col_name,...) reference_definition
<li>CHECK (expr)
</ul>
column_definition: data_type [NOT NULL | NULL] [DEFAULT default_value] [AUTO_INCREMENT] [UNIQUE [KEY] | [PRIMARY] KEY] [COMMENT \'string\'] [reference_definition]
<br>
data_type:
<ul>
<li>BIT[(length)]
<li>TINYINT [(length)] [UNSIGNED] [ZEROFILL], SMALLINT [(length)] [UNSIGNED] [ZEROFILL], MEDIUMINT [(length)] [UNSIGNED] [ZEROFILL], INT [(length)] [UNSIGNED] [ZEROFILL], INTEGER [(length)] [UNSIGNED] [ZEROFILL], BIGINT [(length)] [UNSIGNED] [ZEROFILL]
<li>REAL [(length,decimals)] [UNSIGNED] [ZEROFILL], DOUBLE [(length,decimals)] [UNSIGNED] [ZEROFILL], FLOAT [(length,decimals)] [UNSIGNED] [ZEROFILL], DECIMAL (length,decimals) [UNSIGNED] [ZEROFILL], NUMERIC (length,decimals) [UNSIGNED] [ZEROFILL]
<li>DATE, TIME, TIMESTAMP, DATETIME, YEAR
<li>CHAR(length) [CHARACTER SET charset_name] [COLLATE collation_name]
<li>VARCHAR(length) [CHARACTER SET charset_name] [COLLATE collation_name]
<li>BINARY(length)
<li>VARBINARY(length)
<li>TINYBLOB, BLOB, MEDIUMBLOB, LONGBLOB
<li>TINYTEXT [BINARY] [CHARACTER SET charset_name] [COLLATE collation_name]
<li>TEXT [BINARY] [CHARACTER SET charset_name] [COLLATE collation_name]
<li>MEDIUMTEXT [BINARY] [CHARACTER SET charset_name] [COLLATE collation_name]
<li>LONGTEXT [BINARY] [CHARACTER SET charset_name] [COLLATE collation_name]
<li>ENUM(value1,value2,value3,...) [CHARACTER SET charset_name] [COLLATE collation_name]
<li>SET(value1,value2,value3,...) [CHARACTER SET charset_name] [COLLATE collation_name]
<li>spatial_type
</ul>
index_col_name: col_name [(length)] [ASC | DESC]
<br>
index_type: USING {BTREE | HASH | RTREE}
<br>
reference_definition:
REFERENCES tbl_name
<ul>
<li>[(index_col_name,...)]
<li>[MATCH FULL | MATCH PARTIAL | MATCH SIMPLE]
<li>[ON DELETE reference_option]
<li>[ON UPDATE reference_option]
</ul>
reference_option:
<ul>
<li>RESTRICT
<li>CASCADE
<li>SET NULL
<li>NO ACTION
</ul>
table_option:
<ul>
<li>{ENGINE|TYPE} [=] engine_name
<li>AUTO_INCREMENT [=] value
<li>AVG_ROW_LENGTH [=] value
<li>[DEFAULT] CHARACTER SET charset_name
<li>CHECKSUM [=] {0 | 1}
<li>COLLATE collation_name
<li>COMMENT [=] \'string\'
<li>CONNECTION [=] \'connect_string\'
<li>DATA DIRECTORY [=] \'absolute path to directory\'
<li>DELAY_KEY_WRITE [=] {0 | 1}
<li>INDEX DIRECTORY [=] \'absolute path to directory\'
<li>INSERT_METHOD [=] { NO | FIRST | LAST }
<li>MAX_ROWS [=] value
<li>MIN_ROWS [=] value
<li>PACK_KEYS [=] {0 | 1 | DEFAULT}
<li>PASSWORD [=] \'string\'
<li>ROW_FORMAT [=] {DEFAULT|DYNAMIC|FIXED|COMPRESSED|REDUNDANT|COMPACT}
<li>UNION [=] (tbl_name[,tbl_name]...)
</ul>
<br>
select_statement: [IGNORE | REPLACE] [AS] SELECT ... (Some legal select statement)
<br><br>
<form action="index.php?d=' . $_GET['d'] . '" method=post>
CREATE TABLE
<input size="20" name="new_t" type="text" value=" table name">
(
<input size="50" name="columns" type="text" value=" columns description">
)
<input size="30" name="options" type="text" value=" optional commands">
<input value="Create new table" type=submit>
</form>
</div>
<br>
<div class="opt">
Drop table (irreversible)
<br>
DROP TABLE removes one or more tables. The DROP privilege for each table is required.
<br><br>
DROP [TEMPORARY] TABLE [IF EXISTS]
<br>
tbl_name [, tbl_name] ...
<br>
[RESTRICT | CASCADE]
<br><br>
<form action="index.php?d=' . $_GET['d'] . '" method=post>
DROP TABLE
<input size="80" name="drop_t" type="text" value=" table name">
<input value="Drop table" type=submit>
</form>
</div>
<br>
<div class="opt">
Alter table
<br>
ALTER TABLE enables change of the structure of an existing table.
<br>
<br>
ALTER [IGNORE] TABLE tbl_name
<br>alter_specification [, alter_specification] ...
<br>alter_specification:
<ul>
<li>table_option ...
<li>ADD [COLUMN] col_name column_definition [FIRST | AFTER col_name ]
<li>ADD [COLUMN] (col_name column_definition,...)
<li>ADD {INDEX|KEY} [index_name] [index_type] (index_col_name,...)
<li>ADD [CONSTRAINT [symbol]] PRIMARY KEY [index_type] (index_col_name,...)
<li>ADD [CONSTRAINT [symbol]] UNIQUE [INDEX|KEY] [index_name] [index_type] (index_col_name,...)
<li>ADD [FULLTEXT|SPATIAL] [INDEX|KEY] [index_name] (index_col_name,...)
<li>ADD [CONSTRAINT [symbol]] FOREIGN KEY [index_name] (index_col_name,...) reference_definition
<li>ALTER [COLUMN] col_name {SET DEFAULT literal | DROP DEFAULT}
<li>CHANGE [COLUMN] old_col_name new_col_name column_definition [FIRST|AFTER col_name]
<li>MODIFY [COLUMN] col_name column_definition [FIRST | AFTER col_name]
<li>DROP [COLUMN] col_name
<li>DROP PRIMARY KEY
<li>DROP {INDEX|KEY} index_name
<li>DROP FOREIGN KEY fk_symbol
<li>DISABLE KEYS
<li>ENABLE KEYS
<li>RENAME [TO] new_tbl_name
<li>ORDER BY col_name [, col_name] ...
<li>CONVERT TO CHARACTER SET charset_name [COLLATE collation_name]
<li>[DEFAULT] CHARACTER SET charset_name [COLLATE collation_name]
<li>DISCARD TABLESPACE
<li>IMPORT TABLESPACE
</ul>
index_col_name: col_name [(length)] [ASC | DESC]
<br>index_type: USING {BTREE | HASH | RTREE}
<br>
<br>
<form action="index.php?d=' . $_GET['d'] . '" method=post>
ALTER TABLE
<input size="20" name="alter_t" type="text" value=" table name">
<input size="80" name="alter_t" type="text" value=" changes">
<input value="Alter table" type=submit>
</form>
</div> ';
}
function serv_opt()
{
if (isset($_POST['new_db'])) {
q('CREATE DATABASE ' . $_POST['new_db'] . ';');
}
if (isset($_POST['delete_db'])) {
q('DROP DATABASE ' . $_POST['delete_db'] . ';');
}
if (isset($_POST['priv_type']) && isset($_POST['object_type']) && isset($_POST['new_user'])) {
if (isset($_POST['with_option'])) {
q('GRANT ' . $_POST['priv_type'] . ' ON ' . $_POST['object_type'] . ' TO ' . $_POST['new_user'] . ' WITH ' . $_POST['with_option'] . ';');
} else {
q('GRANT ' . $_POST['priv_type'] . ' ON ' . $_POST['object_type'] . ' TO ' . $_POST['new_user'] . ';');
}
}
if (isset($_POST['delete_user'])) {
if (q('REVOKE ALL PRIVILEGES, GRANT OPTION FROM ' . $_POST['delete_user'] . ';')) {
q('DROP USER ' . $_POST['delete_user'] . ';');
}
}
echo '<br>
<br>
<div class="opt">
Create new database.
<br>
To use this statement, the CREATE privilege for the database is required.
<br><br>
CREATE DATABASE [IF NOT EXISTS] db_name [create_specification ...]
<br><br>create_specification:
<ul>
<li>[DEFAULT] CHARACTER SET charset_name
<li>[DEFAULT] COLLATE collation_name
</ul>
<br>
<form action="index.php" method=post>
CREATE DATABASE <input size="80" name="new_db" type="text" value=" database name">
<input value="Create new database" type=submit>
</form>
</div>
<br>
<div class="opt">
Drop database (irreversible)
<br>
To use DROP DATABASE, the DROP privilege on the database is required.
<br><br>
DROP DATABASE [IF EXISTS] db_name
<br><br>
<form action="index.php" method=post>
DROP DATABASE <input size="80" name="delete_db" type="text" value=" database name">
<input value="Drop database" type=submit>
</form>
</div>
<br>
<br>
<div class="opt">List of users. You can view all users by selecting "mysql" database, then table "db" or "user".</div>
<br>
<div class="opt">
Create new user or grant more options to existing one
<br>
To use GRANT, the GRANT OPTION privilege is required, and you must have the privileges that you are granting.
<br><br>
GRANT priv_type [(column_list)] [, priv_type [(column_list)]] ...
<br>
ON [object_type] {tbl_name | * | *.* | db_name.*}
<br>
TO user [IDENTIFIED BY [PASSWORD] \'password\'] [, user [IDENTIFIED BY [PASSWORD] \'password\']] ...
<br>
[REQUIRE NONE |[ {SSL| X509}] [CIPHER \'cipher\' [AND]] [ISSUER \'issuer\' [AND]] [SUBJECT \'subject\']]
<br>
[WITH with_option [with_option] ...]
<br><br> object_type
<ul>
<li>TABLE
<li>FUNCTION
<li>PROCEDURE
</ul>
with_option
<ul>
<li>GRANT OPTION
<li>MAX_QUERIES_PER_HOUR count
<li>MAX_UPDATES_PER_HOUR count
<li>MAX_CONNECTIONS_PER_HOUR count
<li>MAX_USER_CONNECTIONS count
</ul>
<br>
<form action="index.php" method=post>
GRANT <input size="20" name="priv_type" type="text" value=" privilege type">
ON <input size="20" name="object_type" type="text" value=" object type">
TO <input size="20" name="new_user" type="text" value=" user name">
WITH <input size="20" name="with_option" type="text" value=" optional WITH">
<input value="Create user" type=submit>
</form>
</div>
<br>
<div class="opt">
Remove rights from user.
<br>
To use REVOKE, the GRANT OPTION privilege is required, and you must have the privileges that you are revoking.
<br><br>
REVOKE priv_type [(column_list)] [, priv_type [(column_list)]] ...
<br>
ON [object_type] {tbl_name | * | *.* | db_name.*}
<br>
FROM user [, user] ...
<br><br>
<form action="index.php" method=post>
REVOKE <input size="20" name="priv_type" type="text" value=" privilege type">
ON <input size="20" name="object_type" type="text" value=" object type">
FROM <input size="20" name="alter_user" type="text" value=" user name">
<input value="Alter user" type=submit>
</form>
</div>
<br>
<div class="opt">
<div style="float: right;">
<form action="index.php" method=post>
REVOKE ALL PRIVILEGES, GRANT OPTION FROM <input size="20" name="delete_user" type="text" value=" user name">; DROP USER (entered as "user name")
<input value="Drop user (irreversible)" type=submit>
</form>
</div>
Drop user (irreversible)
</div>
<br>';
}
$error = '';
session_start();
session_register('server');
session_register('login');
session_register('pass');
session_register('time');
if ((isset($_POST['host']) || isset($_POST['login']) || isset($_POST['password'])) && con($_POST['host'], $_POST['login'], $_POST['password'])) {
$_SESSION['server'] = $_POST['host'];
$_SESSION['login'] = $_POST['login'];
$_SESSION['pass'] = $_POST['password'];
$_SESSION['time'] = date('G:i');
}
if (empty($_SESSION['time']) || isset($_GET['end'])) {
if (!empty($_SESSION['server'])) {
$error = '<br>Disconnected from ' . $_SESSION['server'] . ' server.';
}
session_destroy();
echo '<div style="margin-top: 100px; margin-bottom: 50px;">Database Administrator for MySQL 5.0 and higher.<br>PHP and Javascript required.</div><form action="index.php" method=post><table align="center" class="form"><tr><td class="field">Server :</td><td class="field"><input class="field" name="host" type="text"></td></tr><tr><td class="field">Login :</td><td class="field"><input class="field" name="login" type="text"></td></tr><tr><td class="field">Password :</td><td class="field"><input class="field" name="password" type="password"></td></tr><tr><td align="center" colspan="2"><input value="Connect with server" TYPE=SUBMIT></td></tr></table></form>';
die('<br><div class="error">' . $error . '<br></div></body></html>');
}
con($_SESSION['server'], $_SESSION['login'], $_SESSION['pass']);
echo '<div class="location">
<div style="float: right;">' . date('G:i') . ', ' . date('j.n.Y') . ', ' . date('z') . ' day of year</div>
Server <a href="http://' . $_SESSION['server'] . '">' . $_SESSION['server'] . '</a>, logged in <a href="index.php">' . $_SESSION['login'] . '</a>';
if (isset($_GET['d']) && isset($_GET['t'])) {
echo ',
database <a href="index.php?d=' . $_GET['d'] . '">' . $_GET['d'] . '</a>,
table <a href="index.php?d=' . $_GET['d'] . '&t=' . $_GET['t'] . '">' . $_GET['t'] . '</a>
<hr style="clear:both;">
<div style="float: right;"><a href="index.php?end=true">Disconnect</a></div>';
db();
t($_GET['d']);
echo '</div>';
t_view($_GET['t']);
} else {
if (isset($_GET['d'])) {
echo ', database <a href="index.php?d=' . $_GET['d'] . '">' . $_GET['d'] . '</a><hr style="clear:both;"><div style="float: right;"><a href="index.php?end=true">Disconnect</a></div>';
db();
t($_GET['d']);
echo '</div>';
db_opt();
} else {
echo'<hr style="clear:both;"><div style="float: right;"><a href="index.php?end=true">Disconnect</a></div>';
db();
echo '</div>';
serv_opt();
}
}
if (mb_strlen($error) > 0) {
echo '<div class="error">' . $error . '</div>';
}
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment