Forum Moderators: coopster

Message Too Old, No Replies

Creating mysql database via browser

         

kosie79

3:33 am on Jan 22, 2004 (gmt 0)

10+ Year Member



Following is a program to create database via a browser, However when i click the submit, the input box is cleared and i dun see anything. Anybody able to help? Or what other easier method is there to create a mysql database?

<!-- 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>

coopster

10:40 am on Jan 22, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, kosie79!

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...



Do you have these variable assignments mixed up? I think you mean for them to be:

$user="root";
$host="localhost";



You may need to be checking the $_GET [php.net] superglobal array for your $form variable. It is one of PHP's Predefined variables [php.net]:

if ($_GET['form'] == "yes")



>>...to create database via a browser...

mysql_select_db($database);

If you are able to select the database, then you don't need to create one ;)


More variable issues. This time it is your POSTed data. You may need to be checking the $_POST [php.net] superglobal array. It is also one of PHP's Predefined variables [php.net]:

$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);



That should get you started. Once again, Welcome to WebmasterWorld!

kosie79

7:53 am on Jan 25, 2004 (gmt 0)

10+ Year Member



Hi thanks:)

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>

coopster

10:52 pm on Jan 25, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Yes, there are many tools to assist you in creating databases and tables, including phpmyadmin and the other tools mentioned earlier. I prefer the command line myself, and yes, we can help you with that too. But, that said, sooner or later you are going to want to write some PHP code to use that database and I feel you have a good start here and something to learn from. Your choice, though.

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.