<?php session_start(); //must be at beginning ?>

<html>
<head>
<title>PHP MySQL client</title>
</head>

<!-- presents generic MySQL prompt on browser -->

<body onload="document.forms[0].elements[0].focus();">


<?php
//initial page load.  get username and password
if ($_SERVER['REQUEST_METHOD'] == 'GET'): ?>
login to MySQL database server:<br>
<form name="loginForm" action="https://<?php echo $_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'] ?>" method="post">
Username:
<input type="text" size="12" maxlength="20"  name="username">
<br>
Password:
<input type="password" size="12" maxlength="30"  name="password">
<br>
<input name="login" value="Login" type="submit">
</form>
</body>
</html>
<?php
$_SESSION
['firstposting'] = true;  //HTTP_SESSION_VARS is pre 4.1
exit();  
endif;
?>


<?php
if ($_SESSION['firstposting']) {
  
$_SESSION['username'] = $_POST['username'];
  
$_SESSION['password'] = $_POST['password'];
  
$_SESSION['curr_DB'] = "";
  
$_SESSION['queryHistory'] = "";     // \0 delimited fields
  
$_SESSION['firstposting'] = false;
}

//echo "username: ".$_SESSION['username']."   password: ".$_SESSION['password'];
$dbcnx = @mysql_connect("localhost", $_SESSION['username'], $_SESSION['password']);

if (!
$dbcnx) {
  echo
"<p>Unable to connect to the database server.</p>";
  exit();
}

if (
$_SESSION['currDB']!="")
  if (!
mysql_select_db($_SESSION['currDB']) ) {
    echo
"Unable to access the ".$_SESSION['currDB']." database. try another.";
    
$_SESSION['currDB'] = "";    //so next iteration starts blank
  
}
?>


<form name="queryForm" action="https://<?php echo $_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'] ?>" method="post">
<?php
echo "MySQL Server ".mysql_get_server_info();
echo
"@ ".mysql_get_host_info();
echo
"&nbsp;&nbsp;&nbsp;Client: ".mysql_get_client_info();
?>
<br>
MySQL server command-&gt;
<br>
<textarea name="cmd" rows="5" cols="100" wrap="hard"><?php echo $_POST['cmd'] ?></textarea>
<br>
Query history:
<select name="previousQueries"
onChange="document.queryForm.cmd.value =  document.queryForm.previousQueries.options[document.queryForm.previousQueries.selectedIndex].value;">
<?php
$history
= explode('\0',$_SESSION['queryHistory']);
$histsize = count($history);
for (
$i=$histsize-1; $i>=0; $i--)
     echo
"<option>".$history[$i]."</option>\n";
?>
</select>
<br>
<input type="submit">    <!-- if this is before select, the onChange does not work?!?! -->
</form>

<?php
$_POST
['cmd'] = trim($_POST['cmd']);

if (
strlen($_POST['cmd'])>0) {
  if (
eregi( "^ *USE  *([[:alnum:]]+)",$_POST['cmd'],$regs)) {
    
$_SESSION['currDB'] = $regs[1];
    if (!
mysql_select_db($_SESSION['currDB']) )  {
      echo
"Unable to access the ".$_SESSION['currDB']." database.  try another.";
      
$_SESSION['currDB'] = "";
    }
    else {    
//successful USE db command, add to history
      
$_SESSION['queryHistory'] .= '\0'.$_POST['cmd'];
    }
    echo
"<br>Current database: ".$_SESSION['currDB']."\n";
    echo
"<hr>\n";
  }
  else {     
//not a USE command...
    
echo "Current database: ".$_SESSION['currDB']."\n";
    echo
"<hr>\n";
    
$result = mysql_query($_POST['cmd']);
    if (!
$result)
      echo
"<P>Error performing command: ".$_POST['cmd']."<br>" . mysql_error() . "</P>";
    else {
      echo
"<h3>".$_POST['cmd']."</h3>\n";
      echo(
"<table border=1>\n");
      echo(
"<tr>");
      for (
$i=0; $i<mysql_num_fields($result); $i++) {
    echo
"<th>".mysql_field_name($result,$i)."</th>";
      }
      echo(
"</tr>\n");

      while (
$row = mysql_fetch_row($result)) {
    
$fields = count($row);
    echo(
"<tr>");
    for (
$i=0; $i<$fields; $i++)
      echo
"<td>".$row[$i]."</td>";
    echo(
"</tr>\n");
      }
      echo
"</table>\n";
      
$num_rows = mysql_num_rows($result);
      if (
$num_rows)
    echo
$num_rows." rows in set\n";
      
//mysql_affected_rows() for Insert,Delete,Update statements
      //mysql_info() for some statements

      
$_SESSION['queryHistory'] .= '\0'.$_POST['cmd'];    //add to history
    
}
  }
}
else {   
//empty command
  
echo "Current database: ".$_SESSION['currDB']."\n";
  echo
"<hr>\n";
}
?>

<!--  using session instead
<input type="hidden" name="currDB" value="<?php echo $_POST['currDB'] ?>" >
<input type="hidden" name="username" value="<?php echo $_POST['username'] ?>" >
<input type="hidden" name="password" value="<?php echo $_POST['password'] ?>" >
-->

</body>
</html>