Forum Moderators: open
<?php
function getInfo($sql)
{
$dbuser = "user";
$dbpass = "pass";
$dbname = "localhost";
$db = mysql_connect($dbname,$dbuser,$dbpass);
$dbase = @mysql_select_db("mydb", $db) or die("Unable to select database.");
if (!$dbase)
{
exit("Unable to connect to database: " . $dbase);
}
$res = mysql_query($sql, $db);
mysql_close($db);
return $res;
}
?>
<?php
require("../database.php");
// contains method getInfo("sql") which returns the record
?>
<html>
<head>
<title>SQL Execute</title>
</head>
<body>
<form action="?" method="post">
<textarea name="query" cols="20" rows="5"></textarea><br>
<input type="submit" value="Submit">
</form>
<?php
$query = $_POST['query'];
if (isset($query))
{
$query = stripslashes($query);
if (stristr(" " . $query,'SELECT')) // Working with PHP 4...
{
echo "Query: " . $query . "<br>";
echo "<table border=\"1\" cellpadding=\"3\">";
echo "<tr>";
$rs = getInfo($query);
if (!$rs)
{
exit("Error in query: " . mysql_error());
}
for ($i=0; $i<mysql_num_fields($rs); $i++)
{
echo "<th>" . mysql_field_name($rs,$i) . "</th>";
}
echo "</tr>";
while ($arr = mysql_fetch_array($rs, MYSQL_NUM))
{
echo "<tr>";
for ($j=0; $j<count($arr); $j++)
{
echo "<td>" . $arr[$j] . "</td>";
}
echo "</tr>";
}
echo "</table>";
}
else
{
echo $query . "<br>";
$rs = getInfo($query);
echo "Result: ";
if ($rs == TRUE)
echo "Successful";
else if ($rs == FALSE)
echo "Unsuccessful: " . mysql_error();
else
echo "'" . $rs . "'";
}
}
?>
</body>
</html>
[edited by: webfoo at 5:33 pm (utc) on Feb 26, 2011]