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
https://forums.phpfreaks.com/topic/240848-php-create-database/
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
https://forums.phpfreaks.com/topic/240848-php-create-database/#findComment-1237120
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
https://forums.phpfreaks.com/topic/240848-php-create-database/#findComment-1237125
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
https://forums.phpfreaks.com/topic/240848-php-create-database/#findComment-1237127
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
https://forums.phpfreaks.com/topic/240848-php-create-database/#findComment-1237131
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
https://forums.phpfreaks.com/topic/240848-php-create-database/#findComment-1237172
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.