Jump to content

PHP script to create a database


satal keto

Recommended Posts

Hi, I'm doing a project at the moment which requires the use of being able to create a database automatically when ever needed.

So obviously creating a PHP script to get all the needed information and then create the database would be perfect.

 

Here's the code I'm using

 

<?php
/////////////////////////
// MySQL Login details //
/////////////////////////
$host = "localhost";
$user = ""; 
$password = ""; 
////////////////////////////////
// End of MySQL Login details //
////////////////////////////////

////////////////////////
// Defining $database //
////////////////////////
$database = $_COOKIE['fusion_user'];
$bad_details = array(".");
$good_details = array("");
$database = str_replace($bad_details, $good_details, $database);
$database = $database{0};
$database = "database_$database";
// Used to test if database correct
echo $database. "<p>";
// End of used to test if database correct
///////////////////////////////
// End of defining $database //
///////////////////////////////

//////////////////////////////////////
// Create Database if doesn't exist //
//////////////////////////////////////
$conn = mysql_connect($host , $user , $password) or die( mysql_errno().': '.mysql_error() );

$query = "CREATE DATABASE IF NOT EXIST $database";

$result = mysql_query($query, $conn)
    or die(mysql_error());
echo "$result hi";
mysql_close($conn); 
/////////////////////////////////////////////
// End of Create Database if doesn't exist //
/////////////////////////////////////////////


?>

 

I personally can't see anything wrong with that code, but yet it comes up with this response.

 

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]database_1

 

Access denied for user: 'username@localhost' to database 'database_1'

 

The problem is this script uses my highest privilage account. But yet this account cannot create the database. I know that its not that im not supposed to be creating database's (i have unlimited number of database's as part of my hosting).

 

I was wondering if anyone could see any reason why this would be happening, I dont think its the code, but i want to make 100% certain before I start talking to my hosting company about this.

 

Thanks alot

Satal Keto

Link to comment
Share on other sites

First, I thought it was "IF NOT EXISTS", plural, but I could be mistaken. Second, you need the CREATE privilege to do this. The manual has a very funny sounding line in it:

 

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]CREATE DATABASE creates a database with the given name. To use CREATE DATABASE, you need the CREATE privilege on the database.

 

What this means that is that you need to GRANT your user all of the DB-level privileges for your newly "named" DB _before_ you try and CREATE it using that user in order for this to work. This won't happen automatically. If you read on in the MySQL documentation, you'll see what I mean:

 

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]MySQL allows you to grant privileges even on database objects that do not exist. In such cases, the privileges to be granted must include the CREATE privilege. This behavior is by design, and is intended to allow the database administrator to prepare user accounts and privileges for database objects that are to be created at a later time.

 

Try something like (UNTESTED):

 

GRANT ALL ON db_name.* TO 'username@localhost'

 

Hope that helps.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.