PHPBuilder - PHP SQL query tester



RSS Twitter
Snippets Databases

PHP SQL query tester

by: Seth Buntin
|
August 4, 2003

Version: 1.01

Type: Full Script

Category: Databases

License: GNU General Public License

Description: Test your queries with this script that returns the results in table form.



<!-- There are two sections of this file each one needs to be its own page with the names as they are stated -->


<!-- sqltest.php -->
<font size="2" face="Verdana"><strong><font color="#993399"><u>PHP 
      SQL Code Tester</u></font><br>
      </strong></font>
	  <?php
	$host = "localhost";
	$user = "root";
	$password = "";
?>

<form action="mysql_test.php" method="post">
Please select the database for the query:<br><br>
<select name="database" size="1">
<?php
	mysql_connect($host, $user, $password);
	$db_table = mysql_list_dbs();
	for($i = 0; $i < mysql_num_rows($db_table); $i++){
		echo("<option>" . mysql_tablename($db_table, $i));
	}
?>
</select><br><hr>
Please input the SQL query to be executed:<br><br>
<textarea name="query" cols="50" rows="10"></textarea>
<br><br>
<input type="Submit" value="Execute query!">
</form>


<!-- mysql_test.php -->
<font size="2" face="Verdana"><strong><font color="#993399"><u>PHP 
      SQL Code Tester</u></font> - Results<br>
      </strong></font>
	  
		<?php
			$user = "root";
			$host = "localhost";
			$password = "";
			
			mysql_connect($host, $user, $password);
			mysql_select_db($_POST['database']);
			$query = stripslashes($_POST['query']);
			$result = mysql_query($_POST['query']);
		?>
		
		<?php echo "Results of query <b>" . $_POST['query'] . "</b><hr>"; ?>
		
		<?php
			if($result == 0):
				echo("<b>Error " . mysql_errno() . ": " . mysql_error() . "</b>");
			elseif(mysql_num_rows($result) == 0):
				echo("<b>Query executed successfully!</b>");
			else:
		?>
		
		<table border="1">
			<thead>
				<tr bgcolor="#CCCCCC">
					<?php
						for($i = 0; $i < mysql_num_fields($result); $i++){
							echo("<th><font size=\"1\" face=\"Verdana\">" . mysql_field_name($result, $i) . "</font></th>");
							}
					?>	
				</tr>	
			</thead>
			<tbody>
				<?php
					for($i = 0; $i < mysql_num_rows($result); $i++){
						echo ("<tr>");
						$row_array = mysql_fetch_row($result);
						for($j = 0; $j < mysql_num_fields($result); $j++){
							echo("<td><font size=\"1\" face=\"Verdana\">" . $row_array[$j] . "</font></td>");
						}
						echo("</tr>");
					}
				?>
			</tbody>
		</table>
		<?php
			endif;
		?>
		
		<hr><br>
		<form action="query.php" method="post">
			<input type="submit" value="New query">
		</form>

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Seth Buntin

Comment:



Comment:

(Maximum characters: 1200). You have characters left.