Forum Moderators: coopster
<!-- Program Name: mysql_send.php
Description: PHP program that sends an SQL query to the
MySQL server and displays the results.
-->
<html>
<head>
<title>SQL Query Sender</title>
</head>
<body>
<?php
$user="localhost";
$host="root";
$password="";
/* Section that executes query */
if (@$form == "yes")
{
mysql_connect($host,$user,$password);
mysql_select_db($database);
$query = stripSlashes($query) ;
$result = mysql_query($query);
echo "Database Selected: <b>$database</b><br>
Query: <b>$query</b>
<h3>Results</h3>
<hr>";
if ($result == 0)
echo("<b>Error " . mysql_errno() . ": " . mysql_error() . "</b>");
elseif (@mysql_num_rows($result) == 0)
echo("<b>Query completed. No results returned.</b><br>");
else
{
echo "<table border='1'>
<thead>
<tr>";
for ($i = 0; $i < mysql_num_fields($result); $i++)
{
echo("<th>" . mysql_field_name($result,$i) . "</th>");
}
echo " </tr>
</thead>
<tbody>";
for ($i = 0; $i < mysql_num_rows($result); $i++)
{
echo "<tr>";
$row = mysql_fetch_row($result);
for ($j = 0; $j < mysql_num_fields($result); $j++)
{
echo("<td>" . $row[$j] . "</td>");
}
echo "</tr>";
}
echo "</tbody>
</table>";
}
echo "<hr><br>
<form action=$PHP_SELF method=post>
<input type=hidden name=query value=\"$query\">
<input type=hidden name=database value=$database>
<input type=submit name=\"queryButton\" value=\"New Query\">
<input type=submit name=\"queryButton\" value=\"Edit Query\">
</form>";
unset($form);
exit();
}
/* Section that requests user input of query */
@$query = stripSlashes($query);
if (@$queryButton!= "Edit Query")
{
$database = " ";
$query = " ";
}
?>
<form action=<?php echo $_SERVER['PHP_SELF']?>?form=yes method="post">
<table>
<tr>
<td align="right"><b>Type in database name</b></td>
<td>
<input type=text name="database" value=<?php echo $database?> >
</td>
</tr>
<tr>
<td align="right" valign="top"><b>Type in SQL query</b></td>
<td><textarea name="query" cols="60" rows="10"><?php echo $query?></textarea>
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Submit Query"></td>
</tr>
</table>
</form>
</body>
</html>
I'm a command line user but there are quite a few options when it comes to MySQL Contributed software [mysql.com] -- applications/APIs developed by third parties. One of the more popular on this board seems to be phpMyAdmin ( h**p://www.phpmyadmin.net/home_page/ ).
However, I see a few things in your script that may be causing issues for you...
$user="root";
$host="localhost";
if ($_GET['form'] == "yes")
mysql_select_db($database);
$database = (isset($_POST['database']))? $_POST['database'] : '';
$db = mysql_select_db [php.net]($database);
if (!$db) exit('Houston, we have a problem!'); // You should add error checking...
$query = (isset($_POST['query']))? $_POST['query'] : '';
$query = stripslashes($query);
I have made the necessary changes...as follows but it still gives me parse errors:( anyway this program is basically to create the database rite.? i could do withouth this..if i can learn how to create the database without web base...
How about mysqlfront..or could you guide me on how to create the database with command line? im totally new in this:(
<html>
<head>
<title>SQL Query Sender</title>
</head>
<body>
<?php
$user="root";
$host="localhost";
$password="";
/* Section that executes query */
if ($_GET ['form'] == "yes")
{
mysql_connect($host,$user,$password);
mysql_select_db($database);
$query = stripSlashes($query) ;
$result = mysql_query($query);
echo "Database Selected: <b>$database</b><br>
Query: <b>$query</b>
<h3>Results</h3>
<hr>";
if ($result == 0)
echo("<b>Error " . mysql_errno() . ": " . mysql_error() .
"</b>");
elseif (@mysql_num_rows($result) == 0)
echo("<b>Query completed. No results returned.</b><br>");
else
{
echo "<table border='1'>
<thead>
<tr>";
for ($i = 0; $i < mysql_num_fields($result); $i++)
{
echo("<th>" . mysql_field_name($result,$i) . "</th>");
}
echo " </tr>
</thead>
<tbody>";
for ($i = 0; $i < mysql_num_rows($result); $i++)
{
echo "<tr>";
$row = mysql_fetch_row($result);
for ($j = 0; $j < mysql_num_fields($result); $j++)
{
echo("<td>" . $row[$j] . "</td>");
}
echo "</tr>";
}
echo "</tbody>
</table>";
}
echo "<hr><br>
<form action=$PHP_SELF method=post>
<input type=hidden name=query value=\"$query\">
<input type=hidden name=database value=$database>
<input type=submit name=\"queryButton\" value=\"New Query\">
<input type=submit name=\"queryButton\" value=\"Edit Query\">
</form>";
unset($form);
exit();
}
/* Section that requests user input of query */
$query = stripSlashes($query);
if ($queryButton!= "Edit Query");
{
$database=isset($_POST['database'])?$_POST['database'];
$db=mysql_select_db($database);
if(!$db) exit('Houston, we have a problem!');
$query=(isset($_POST['query']))?$_POST['query'];
?>
<form action=<?php echo $_SERVER['PHP_SELF']?>?form=yes method="post">
<table>
<tr>
<td align="right"><b>Type in database name</b></td>
<td>
<input type=text name="database" value=<?php echo $database?> >
</td>
</tr>
<tr>
<td align="right" valign="top"><b>Type in SQL query</b></td>
<td><textarea name="query" cols="60" rows="10"><?php echo
$query?></textarea>
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Submit
Query"></td>
</tr>
</table>
</form>
</body>
</html>
When you receive a parse error like this...
Parse error: parse error, unexpected ';' in /path/to/your/server/yourfile.php on line 67
...PHP is attempting to let you know what and where your issue lies. In this case, your $database variable assignment is incorrect (as is your $query variable three lines later). I often use the ternary [us3.php.net] operator in variable assignment. In this case, you seemed to have keyed it incorrectly. Have a look at those lines in your code, compare to the notes earlier, and you'll see what you did wrong. Also, read the link on the ternary operator. It is going to come in very handy as you are programming and a good understanding of how it works is going to be worth the few minutes of your time.
Oh, one more thing. You may want to get in the habit of indenting your code, especially within logic statements and loops such as the
if and foreach routines you have coded here. You'll be able to quickly identify where you are missing opening braces ({) and closing braces (}), and yes, you do have some. As you are looking at the ternary operator in the manual pages, have a look at how the author's indented their code. And take a look at some of the User Contributed notes to see how others indent their code to keep it clean and easy to maintain. (I just realized that maybe you cut and pasted your code here -- and maybe it is, in fact, indented. If so, have a look at your logic as you will notice some errors). Regards, coopster.