#native_company# #native_desc#
#native_cta#

Poor Man’s ISQL (MySQL Style)

By Rodney Johnson
on August 6, 2002

Version: .9

Type: Full Script

Category: Databases

License: GNU General Public License

Description: Much like Yurko’s “SQL Terminal” above, this lets you run raw SQL against the database using a web interface. I use frames for this so standard editbox functions, like “undo typing,” etc. are preserved between requests. It also allows the output to be sent to an excel spreadsheet so that ad hoc queries can be mailed rather quickly to your screaming accounting and marketing people (you know how they love their spreadsheets). This app is usually the first thing I build in order to get used to a new webserver/database environment, and thus was one of my first php scripts when confronted with a php project. It’s a good tool, especially if you’re not at your desk and need a quick way to get at the back end data. Also included is the approximate execution time of the query, very useful when you are tuning the database.

I tend to use long variable names in lieu of heavy commenting and I have a reverse indenting style because it seems to be more readable when word-wrap is turned on. You’ll also notice that I don’t really care about the aesthetics of the resultant HTML. I apologize in advance if this perturbs anyone.

//-------------------------------------------------------------------------
//----------Poor Man's ISQL (MySQL style) in Three files-------------------
//---------------by: http://rodneyj.org------------------------------------
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------


//-------------------------------------------------------------------------
//------------------Page 1:Index.htm (name not important)------------------
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------


<TITLE>Poor Man's ISQL (MySQL style)</TITLE>
<FRAMESET ROWS="33%,*">
   <FRAME SRC="sqlform.php" NAME="title">
   <FRAME SRC="blank.htm" NAME="bottom">
</FRAMESET>
<NOFRAMES>
YO BRO! it's the 21st century already....get it together!!!!
</NOFRAMES>

//-------------------------------------------------------------------------
//------------------Page 2: sqlform.php------------------------------------
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
   <head>
<style type="text/css">
<!--
BODY {font-family: Verdana; font-size: 11px; margin-left: 0; margin-top: 0; margin-right: 0; margin-bottom: 0; line-height: 15px;}
TABLE {font-family: Verdana; font-size: 11px; border: 0; }
		.formsmall { font-family:Verdana, Arial, Ariel,sans-serif; font-size:11px;  font-weight:bold; color:#111111;}
		.formsmallI { font-family:Verdana, Arial, Ariel,sans-serif; font-size:10px; font-style:italic; font-weight:bold; color:#111111;}
 -->
</style>
      <title>Poor Man's ISQL
      </title>
   </head>
   <body bgcolor="#FFFFFF">
      <center>
         <form action="results.php" method="get" target="bottom">
            <table>
			   <caption>
<font face="VERDANA, HELVETICA, ARIAL" size="+2">Poor Man's ISQL</font><br clear="all">			   
			   </caption>
               <tr>
			   	  <td valign=top align=right>
<input type="submit" value="Execute" class="formsmallI"><br clear=all>			  
Results in Spreadsheet:<input type=checkbox name="excel" class="formsmallI">				  
				  </td>
                  <td valign=top align=center>
                     <center>
<font size="-2" face="VERDANA, HELVETICA, ARIAL">
<textarea name="SQLstring" rows="10" cols="100" wrap="virtual" class="formsmall">show tables</textarea><br>
</font>
                     </center>
                  </td>
               </tr>
            </table>
         </form>
      </center>
   </body>
</html>

//-------------------------------------------------------------------------
//------------------Page 3: results.php------------------------------------
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------

<? if($excel) header("Content-type: application/x-msexcel"); ?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
	<title><?=$SQLstring;?></title>
	<style type="text/css"><!--
BODY {font-family: Verdana; font-size: 11px; margin-left: 0; margin-top: 0; margin-right: 0; margin-bottom: 0; line-height: 15px;}
TABLE {font-family: Verdana; font-size: 11px; border: 0; }
	--></style>	
</head>
<body>
<center>
<? 


function db () {
    $dbhandle = mysql_connect( "mySqlHost", "mySqlUser", "mySqlPass" ) or die (mysql_error()); 
    mysql_select_db( "mySqlDatabaseName", $dbhandle ) or die ( mysql_error() ); 
    return $dbhandle;
}

$dbhandle = db();
$skipOutput=0;

$sql=stripslashes($SQLstring);

print "<table border=0 cellspacing=0 cellpadding=1 align=center><tr><td></td><td><b><pre>$sql</pre></b></td></tr>";

$timeStart=gettimeofday();
$timeStart_uS=$timeStart["usec"];
$timeStart_S=$timeStart["sec"];

$result = mysql_query( $sql ) or die ( mysql_error());

$timeEnd=gettimeofday();
$timeEnd_uS=$timeEnd["usec"];
$timeEnd_S=$timeEnd["sec"];
$ExecTime_S=($timeEnd_S+($timeEnd_uS/1000000))-($timeStart_S+($timeStart_uS/1000000));
Print "<tr><td> </td><td align=center>Execution Time: ".($ExecTime_S*1000)." milliseconds</td></tr>";

	if((eregi('delete ',$sql))||(eregi('update ',$sql))||(eregi('insert ',$sql))){
$resultcount = mysql_affected_rows() or die ( mysql_error());
if (!$resultcount) $resultcount = 0;
print "<tr><td> </td><td align=center>affected <b>".$resultcount."</b>  rows</td></tr></table> <br clear=all>";
$skipOutput=1;
	}else{
$resultcount = mysql_num_rows( $result ) or die ( mysql_error());
if (!$resultcount) $resultcount = 0;
print "<tr><td> </td><td align=center>returned <b>".$resultcount."</b>  rows</td></tr></table> <br clear=all>";
	}


print"<table align=center cellspacing=0 cellpadding=1 border=1>";

			if ($resultcount > 0 && !$skipOutput){
			
print "<tr><th>ord</th><td></td>";

	for ($thisField = 0; $thisField < mysql_num_fields($result); $thisField++){
print "<th>&nbsp;".$thisField."</th>";
	}
	
print "</tr>";			
print "<tr><th>row</th><td></td>";

	for ($thisField = 0; $thisField < mysql_num_fields($result); $thisField++){
print "<th>&nbsp;".mysql_field_name($result,$thisField)."</th>";
	}
	
print "</tr>";

		for ($thisRow = 0; $thisRow < mysql_num_rows($result); $thisRow++){
print "<tr><th>".($thisRow+1)."</th><th></th>";
	for ($thisField = 0; $thisField < mysql_num_fields($result); $thisField++){
print "<td>".mysql_result($result,$thisRow,$thisField)."&nbsp;</td>";
	}
print "</tr>";
		}
		
			}
			
print"</table>";

?>
</center>
</body>
</html>

//------------------------------------------------------------------------------
//--------Page 4: blank.htm (oops, netscape needs a frame source for targeting)-
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
	<title>blank</title>
</head>
<body>
</body>
</html>