<?php
$dbcnx
= @mysql_connect("localhost", "wills", "");
if (!
$dbcnx) {
  echo
"<p>Unable to connect to the database server at this time.</p>";
  exit();
}

$dbs = @mysql_query("SHOW DATABASES");
if (!
$dbs) {
  echo
"<p>Error performing SHOW DATABASES: " . mysql_error() . "</p>";
  exit();
}

if (!isset(
$_POST['selectDB'])) {   //first call of program
  
$row = mysql_fetch_row($dbs);  //how to combine into 1 stmt?
  
$db = $row[0];
  
$dbs = @mysql_query("SHOW DATABASES");  //"refresh" for Select...
}
else if (
$_POST['selectDB'] != $_POST['currDB'])   //changed DB
  
$db = $_POST['selectDB'];
else                      
//same as last time
  
$db = $_POST['currDB'];


if (! @
mysql_select_db($db) ) {
  echo
"<p>Unable to access the ".$db." database.  " .
       
"Go back and try another.</p>";
  exit();
}


$tables = @mysql_query("SHOW TABLES");
if (!
$tables) {
  echo
"<p>Error performing SHOW TABLES: " . mysql_error() .
       
"Go back and try another</p>";
  exit();
}

//first call of program or changed DB
if (!isset($_POST['selectTable']) || ($_POST['selectDB'] != $_POST['currDB'])) {   
  
$row = mysql_fetch_row($tables);  //how to combine into 1 stmt?
  
$table = $row[0];
  
$tables = @mysql_query("SHOW TABLES");  //"refresh" for Select...
}
else
  
$table = $_POST['selectTable'];

$describe = @mysql_query("DESCRIBE ".$table);
if (!
$describe) {
  echo
"<p>Error performing DESCRIBE: " . mysql_error() . "</p>";
  exit();
}

echo
"<form action=$_SERVER[PHP_SELF] method='POST'>";
echo
"<table width='30%'><tr><td>";
echo
"<select name='selectDB'>";
while (
$row = mysql_fetch_row($dbs))
  if (
$row[0] == $db)       //DB state saved
    
echo "<option selected>".$row[0];
  else
    echo
"<option>".$row[0];
echo
"</select>";
echo
"<td>";
echo
"<input type='submit' value=' Change Database'>";

//echo "<br>";
echo "<tr><td>";
echo
"<select name='selectTable'>";
while (
$row = mysql_fetch_row($tables))
  if (
$row[0] == $table)       //table state saved
    
echo "<option selected>".$row[0];
  else
    echo
"<option>".$row[0];
echo
"</select>";
echo
"<td>";
echo
"<input type='submit' value=' Change Table'>";
echo
"</table>";

echo
"<table border=1 width='50%'>";
echo
"<caption><b>$table table structure:</b></caption>";
echo
"<tr><th align='left'>Attribute name</th><th align='left'>Type</th></tr>";
while (
$row = mysql_fetch_row($describe) ) {
  echo
"<tr><td>".$row[0]."<td>".$row[1]."</tr>";
//  $fields++;
}
echo
"</table>";
?>

SELECT<input type="text" size="40"  name="query">
FROM <?php echo $table; ?>
<input type="text" size="40" name="querysuffix">
<br>
<input type="submit" value=" Submit query">
<p>


<input type="hidden" name="currDB" value=<?php echo $db ?> >
<input type="hidden" name="currTable" value=<?php echo $table ?> >
</form>

<hr>

<?php
if (strlen($_POST['query'])>0) {
  
$select = "SELECT ".$_POST['query']." FROM ".$table." ".$_POST['querysuffix'];
  
$result = mysql_query($select);
  if (!
$result) {
    echo(
"<P>Error performing query: $select <br>" . mysql_error() . "</P>");
    exit();
  }
  else {
    echo
"<h3>Results for: $select</h3>";
    echo(
"<pre>");

    while (
$row = mysql_fetch_row($result)) {
      
$fields = count($row);
      for (
$i=0; $i<$fields; $i++)
        echo
$row[$i]."\t";
      echo
"<br>";
    }
    echo
"</pre>";
  }
}
?>