Drummin Posted July 1, 2011 Share Posted July 1, 2011 I was hoping to use a script to create a database with a script for a project. I've tried several variations of this without success. Any Ideas? Thanks. $dbhost='localhost'; $user='dbuser'; $password='mypassword'; $dbname='test'; $link = mysql_connect($dbhost, $user, $password); if (!$link) { die('Could not connect: ' . mysql_error()); } $sql = "CREATE DATABASE $dbname"; if (mysql_query($sql, $link)) { echo "Database $dbname created successfully\n"; } else { echo 'Error creating database: ' . mysql_error() . "\n"; } Quote Link to comment Share on other sites More sharing options...
Eiolon Posted July 1, 2011 Share Posted July 1, 2011 Not exactly sure why it's not working for you. I copied and pasted it and it works fine. Just make sure you put the <?php ?> brackets in. Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 1, 2011 Share Posted July 1, 2011 What errors are you experiencing? Quote Link to comment Share on other sites More sharing options...
Drummin Posted July 1, 2011 Author Share Posted July 1, 2011 Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'dbuser'@'localhost' (using password: YES) in C:\Program Files\EasyPHP-5.3.3\www\test24.php on line 11 Could not connect: Access denied for user 'dbuser'@'localhost' (using password: YES) Line 11 is $link = mysql_connect($dbhost, $user, $password); Quote Link to comment Share on other sites More sharing options...
Eiolon Posted July 1, 2011 Share Posted July 1, 2011 Looks like a user permissions issue, not a code issue. I created a user called dbuser with mypassword and granted all privileges. Works fine. Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 1, 2011 Share Posted July 1, 2011 Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'dbuser'@'localhost' (using password: YES) in C:\Program Files\EasyPHP-5.3.3\www\test24.php on line 11 Could not connect: Access denied for user 'dbuser'@'localhost' (using password: YES) You are providing the wrong credentials for connecting to the database. You need to get/create the right credentials. Quote Link to comment Share on other sites More sharing options...
Drummin Posted July 1, 2011 Author Share Posted July 1, 2011 Can users (name and password) be created via a php script? The idea is for new organizations (after approval) to automatically have their DB created, tables added and needed files copied making the program I've created duplicate. The only stumbling block has been actually making the DB. It would seem "user and password" should be unique for each "client" but a preset user could work I suppose. Any thoughts? Quote Link to comment Share on other sites More sharing options...
Eiolon Posted July 1, 2011 Share Posted July 1, 2011 Technically, yes, you can create a new user account via PHP (look at phpMyAdmin, for example). However, you still have to know a valid user account with the appropriate permissions to create ANYTHING within MySQL. How do you know what usernames/passwords are used for every organization? Create a 1 page PDF showing how to setup a new username and password with a couple of screenshots. Quote Link to comment Share on other sites More sharing options...
Drummin Posted July 1, 2011 Author Share Posted July 1, 2011 Thanks for your help and advice. Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 1, 2011 Share Posted July 1, 2011 As part of your configuration script you can have the user provide a username/password and save the values to a configuration file. You can't have your script "magically" connect to a DB without someone providing the connection info. Think about the havoc a script could cause. Quote Link to comment 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.