im not sure how to make a script that creates a database using my sql
i was thinking of it so that people can create their own database.
then it gives the database user in my sql the correct permissions too. Could somebody please show me how or create one it would be greatly appeciated
Comments
say User/password
how would they read what was in the database ?
they can onyl read it from YOUR host,
would it be to help beginners create DB's on your hosting ?
there is already something that does this
www.phpmyadmin.com
Adult Section Leader :cool:
Pimp
use them to connect,
TIP; use arrays.
Adult Section Leader :cool:
Pimp
<?
$dbname = "mydb";
$dbuser = "myname";
$dbpass = "mypass";
$dbhost = "localhost";
mysql_query("CREATE DATABASE $dbname");
mysql_query("CREATE USER '$dbuser'@'$dbhost' IDENTIFIED BY '$dbpass'");
mysql_query("GRANT ALL PRIVILEGES ON $dbname TO '$dbuser'@'$dbhost' IDENTIFIED BY '$dbpass' WITH GRANT OPTION");
?>
You might want to leave the "WITH GRANT OPTION" bit out, as that allows the new user to grant access to more users.
Then you just pass the four variables ($dbuser etc) from a form.. and don't forget to comment out the four variables at the top! In fact, you can probably just leave $dbhost set to 'localhost' for safety.
(all it does in the browser is a bllank white screen)
[php]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Create a MySQL Database</title>
</head>
<body>
<?
if ($action == "create")
{
$adminuser = "root";
$adminpass = "yourpass";
$link = mysql_connect("localhost", $adminuser, $adminpass);
if (!$link) { die("Could not connect: " . mysql_error()); }
$result = mysql_query("CREATE DATABASE $dbname");
if (!$result) { die("Badness: " . mysql_error()); }
$result = mysql_query("GRANT ALL PRIVILEGES ON $dbname.* TO '$dbuser'@'$dbhost' IDENTIFIED BY '$dbpass' WITH GRANT OPTION");
if (!$result) { die("Badness: " . mysql_error()); }
print ("Success!");
}
else
{
?>
<table width="300" border="0" cellpadding="4">
<form name="dbform" method="post" action="createdb.php">
<tr>
<td colspan="2"><strong>Create a MySQL Database </strong></td>
</tr>
<tr>
<td>Database Name </td>
<td>
<input name="dbname" type="text" id="dbname" /></td>
</tr>
<tr>
<td>Database User </td>
<td><input name="dbuser" type="text" id="dbuser" /></td>
</tr>
<tr>
<td>Database Password </td>
<td><input name="dbpass" type="password" id="dbpass" /></td>
</tr>
<tr>
<td>Database Host
<input name="action" type="hidden" id="action" value="create" /></td>
<td><input name="dbhost" type="text" id="dbhost" value="localhost" /></td>
</tr>
<tr>
<td> </td>
<td>
<input type="submit" name="Submit" value="Go!" />
</td>
</tr>
</form>
</table>
<?
}
?>
</body>
</html>
[/php]
the createdb.php is chmod 777
its so close
this is the command need to make a database and set perrmissions
CREATE DATABASE andrew;
GRANT andrew ON andrew TO penguinsrule
GRANT ALL PRIVILEGES ON andrew.* TO penguin@localhost IDENTIFIED BY 'ctcvyv';
GRANT SELECT,INSERT,UPDATE,DELETE ON andrew.* TO penguin@localhost IDENTIFIED BY 'ctcvyv';
BTW which PHP version is your server?
Offlife Webmaster Directory & Forum
Free Script Installation
M4U.us Free PR1 Link Directory
Off-Life Free PR3 Link Directory