satal keto Posted January 1, 2006 Share Posted January 1, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/3132-php-script-to-create-a-database/ Share on other sites More sharing options...
LazyJones Posted January 1, 2006 Share Posted January 1, 2006 Nothing wrong with the code. Only thing I can come up with is wrong user/pass Quote Link to comment https://forums.phpfreaks.com/topic/3132-php-script-to-create-a-database/#findComment-10528 Share on other sites More sharing options...
fenway Posted January 2, 2006 Share Posted January 2, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/3132-php-script-to-create-a-database/#findComment-10529 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.