Jump to content

php Create Database


Drummin

Recommended Posts

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";
}

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.