Database Creating script

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

  • joshlljoshll Junior Member Shared Hoster
    it wouldnt work, people wouldnt be able to use it perperly, unless it was a specific database for a spcific termenology.
    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 Advisor :cool:
    Adult Section Leader :cool:
    Pimp ;)
  • penguinsrulepenguinsrule Beginner Link Clerk
    no i mean it like you fill in a forum field what is the database name and it creates it when you hit submit and it automaticly sets the permissions to one user what is always the same. Its will be going towards automated forum creating
  • DeluxeNamesDeluxeNames Admin Administrator
    I'm not sure of the answer but I did notice that you just joined today and I wanted to welcome you to the forum Penguinsrule. I will try to find out for you.
  • penguinsrulepenguinsrule Beginner Link Clerk
    thanks for the welcome and its really frustrating that you can have forums but its hard to offer it to people and i just need this hard bit to be solved and im fine all it needs to do is its like a webpage with a text field that you type your database name in and when you hit submit it creates the database.
  • joshlljoshll Junior Member Shared Hoster
    ah, you can use MySQL database strings, in your cPanel MySQL databases section locate the database you want at the top of the page and there you will find the DB strings :)
    use them to connect,

    TIP; use arrays.
    Adult Advisor :cool:
    Adult Section Leader :cool:
    Pimp ;)
  • BeachieBeachie Moderator: Raging Bender Link Clerk
    Just off the top of my head, you could create a PHP script something like:
    <?
    $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.
    Gadget.com | Everybody.com | BulletinBoard.com | Stupidity.com | Aquatic.com | Cuddly.com | Blogs.org | Newcomers.com | Wealth.org
  • penguinsrulepenguinsrule Beginner Link Clerk
    thaanks for the try it doesnt work i think you might need the part where it logins in to mysql
    (all it does in the browser is a bllank white screen)
  • BeachieBeachie Moderator: Raging Bender Link Clerk
    Oh, you'll need to do more than just that code snippet.. you definitely need the mysql login stuff above that, and a bunch of HTML to make the form.. i'll see if i can chuck something together in about 30 mins.
    Gadget.com | Everybody.com | BulletinBoard.com | Stupidity.com | Aquatic.com | Cuddly.com | Blogs.org | Newcomers.com | Wealth.org
  • BeachieBeachie Moderator: Raging Bender Link Clerk
    Save this file as createdb.php. Set $adminuser and $adminpass to a user account with sufficient privileges. I've tested this on one of my cPanel servers running MySQL 4.11
    [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]
    Gadget.com | Everybody.com | BulletinBoard.com | Stupidity.com | Aquatic.com | Cuddly.com | Blogs.org | Newcomers.com | Wealth.org
  • penguinsrulepenguinsrule Beginner Link Clerk
    i take it you tested it because i tryed it and i typed the forum in hit go and it just refreshed and didnt do any thing.
    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';
  • rebelagentrebelagent Junior Member Shared Hoster
    You can try tizag.com they have a pretty good tut on MySQL and PHP. I've learned a lot from them. If you aren't successful there try pixel2life.com they have unlimited supply of tutorials.

    BTW which PHP version is your server?
Sign In or Register to comment.