<?php


$q = stripslashes(trim($_REQUEST['q']));



mysql_connect('dav.cse.ucsc.edu', 'ktaocl', 'chatbot5');
mysql_select_db('backpacking');

$r = mysql_list_tables('backpacking');
$muts = array();
if($r) {
	while($row = mysql_fetch_row($r)) {
		$muts[] = $row[0];
	}
} else {
	$muts = mysql_error();
}

$r = mysql_query($q);

$guts = array();
if($r) {
	while($obj = mysql_fetch_object($r)) {
		$guts[] = $obj;
	}
	$cols = array_keys(get_object_vars($guts[0]));
} else {
	$guts = mysql_error();
}

$futs = mysql_info();

mysql_close();

?>

<html>
  <head>
    <title>therobot - a mysql interface thingyyyy</title>
    <style>
	body {
		font-family: arial;
	}
	td {
		vertical-align: top;
		border: thin solid black;
	}
	code {
		background-color: yellow;
	}
	thead {
		font-weight: bolder;
	}
	tbody {
		font-size: small;
	}
    </style>
  </head>
  <body>

<form>
  <input type="text" name="q" value="<?=$q?>" size=80" />
  <input type="submit" />
</form>

<p>Query: <code><?=$q?></code></p>


<table>
<tr>
<td>
 <table>
  <thead>
   <tr>
<?php 
foreach($cols as $col) {
	print("     <td>$col</td>\n");
}?>
   </tr>
 </thead>
 <tbody>
<?php
foreach($guts as $gut) {
	print("    <tr>\n");
	foreach($cols as $col) {
		if(!$gut->$col) $gut->$col = "&nbsp;";
		print("     <td>".$gut->$col."</td>\n");
	}
	print("    </tr>\n");
}
?>
 </tbody>
</table>
<td>tables<br/><pre><?php foreach($muts as $mut) { print "<a href=\"?q=select+*+from+$mut\">$mut</a> <a href=\"?q=describe+$mut\">?</a>\n"; } ?></pre><br/> <?php print($futs); ?></td>
</tr>
</table>


  </body>
</html>
