Mysql interface

Posted: August 31, 2011 in MySql, PHP
<? session_start();
    set_time_limit(0);
	
	error_reporting(0);	
	if($_POST['act']=='doNow' && isset($_POST['act']))
	{
		$mysqlHost = trim($_POST['mysqlHost']);
		$mysqlUsername = trim($_POST['mysqlUsername']);
		$mysqlPassword = trim($_POST['mysqlPassword']);
		$query = trim($_POST['query']);
		$mysqlDatabase = trim($_POST['mysqlDatabase']);
		
		if($mysqlHost=="" || $mysqlUsername=="" ||  $query=="" || $mysqlDatabase=="")
		{
			$_SESSION['msg'] = '<strong style="color:#CC0000;">Please enter all values.</strong>';
			header('Location:mysql-interface.php');
			exit;
		}		
		/* --------------------------------------------------- Code is used to create a tables ------------------------------- */
		if(!mysql_connect($mysqlHost,$mysqlUsername,$mysqlPassword))
		{
			$_SESSION['msg'] = '<strong style="color:#CC0000;">Mysql connection failed !.</strong>';
			header('Location:mysql-interface.php');
			exit;
		}
		if(!mysql_select_db($mysqlDatabase))
		{
			$_SESSION['msg'] = '<strong style="color:#CC0000;">Mysql database selection failed !.</strong>';
			header('Location:mysql-interface.php');
			exit;
		}
		
		$queryArr = explode(';',$query);
		foreach ($queryArr as $oneQuery)
		{
			$oneQuery = trim($oneQuery);
			if($oneQuery != "")
			{			
				//echo '<br />Query : '.$oneQuery;
				// Perform the query
				$ncon = mysql_query($oneQuery) or die('Error performing query: '.$oneQuery.': <br />' . mysql_error() . '<br /><br />');
				if($ncon)	
					$arr[] = "Success : ".$oneQuery;
				else		
					$arr[] = "Error : ".$oneQuery;
			}
		}
		/* --------------------------------------------------- Code is used to create a tables ------------------------------- */
		$str= '<h4>Log:</h4>'.implode('<br />',$arr);
	}
	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Mysql Interface</title>
<style>
table td{ background-color:#F2F2F2;}
</style>

</head>

<body>
<br /><br /><br />
<? if($_POST['act']=='doNow' && isset($_POST['act'])) { ?>
<table align="center" bgcolor="#CCCCCC" width="900" border="0" cellspacing="1" cellpadding="5">
<tr>
   <td  align="center"><h3>Mysql Interface</h3></td>
 </tr>
 <tr>
   <td align="left"><?=$str;?></td>
 </tr>
 </table>
<? } else { ?>
<form id="form1" name="form1" method="post" action="">
<input type="hidden" name="act" value="doNow" />
<table align="center" bgcolor="#CCCCCC" width="600" border="0" cellspacing="1" cellpadding="5">
 <tr>
   <td colspan="2" align="center"><h3>Mysql Interface</h3></td>
 </tr>
 
 <tr>
    <td width="178" align="right">Mysql Host :</td>
    <td width="399">
      <input type="text" style="width:300px;" name="mysqlHost" />    </td>
  </tr>
  <tr>
    <td align="right">Username :</td>
    <td><input type="text" style="width:300px;" name="mysqlUsername" /></td>
  </tr>
  <tr>
    <td align="right">Password :</td>
    <td><input type="password" style="width:300px;" name="mysqlPassword" /></td>
  </tr>
  <tr>
    <td width="178" align="right">Database Name :</td>
    <td width="399">
      <input type="text" style="width:300px;" name="mysqlDatabase" />    </td>
  </tr>
  <tr>
    <td valign="top" align="right">Query :</td>
    <td><textarea name="query" style="width:300px; height:200px;"></textarea><br />
    [ <strong>Note : </strong> Separate multyple queries with ";" ]</td>
  </tr>
  <? if(isset($_SESSION['msg'])){ ?>
  <tr>
    <td align="right"></td>
    <td><?=$_SESSION['msg'];?></td>
  </tr>  
  <? unset($_SESSION['msg']);} ?>
  <tr>
    <td>&nbsp;</td>
    <td>
      <input type="submit" name="button" id="button" onclick="this.value='Loading...'" value="Submit" />
      <br /> <br />      </td>
  </tr>
</table>
</form>
<? } ?>
</body>
</html>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s