4

I have a php file which connects to mysql database.

I need to create a condition, which if the database is not found, it will create it.

I researched and I found how to create a database with PHP. However, I need to create one with the following criteria:

DB Name, DB User, Password and all privileges granted

Is there a way how I can implement this?

5 Answers 5

2

You can do like this

$connection=mysql_connect('server','username',password);

$db_result=mysql_select_db('database');

if(!$db_result){

 mysql_query("CREATE USER 'stackoverflow'@'localhost' IDENTIFIED BY 'mypass';",$connection); //user

mysql_query("GRANT ALL ON db1.* TO 'stackoverflow'@'localhost');"

mysql_query("CREATE DATABASE my_db",$connection); //create database



}else{

//Database is available 

}
Sign up to request clarification or add additional context in comments.

5 Comments

The OP understands this. He doesn't know how to create the DB privileges and connection properties.
When I run this code, the following error message occurs: Could not connect: Access denied for user 'dbname'@'localhost' (using password: YES). Why is this happening? Any ideas?
looks like your password doesn't not match or user doesn't exists
Yes the user doesn't exist, but I want to create everything from scratch from the code, so that the the one that the client doesn't have to create neither the database, nor the user. Is this possible just from the code?
All right, what I did was to create a common user/password and if the database for this particular application is not found, it first connects with the common user/pass and then creates the database.
1

This'll be a bit of a chicken and egg problem. To connect to MySQL via PHP, you need a valid username/password. But your program may possibly connecting to a database which does not have that user/password configured, so how is it going to be able to connect to create the user/password/database in the first place?

Comments

0

See the MySQL Documentation for CREATE USER and GRANT.

Comments

0
$connection=mysql_connect('server','username',password);
$db_result=mysql_select_db('database');
if(!$db_result){
  mysql_query("CREATE USER 'stackoverflow'@'localhost' IDENTIFIED BY 'mypass';",$connection); //user
  mysql_query("GRANT ALL ON db1.* TO 'stackoverflow'@'localhost');"
  mysql_query("CREATE DATABASE my_db",$connection); //create database
}else{
}

Comments

-1

Here is a cli script i wrote (modified other copy-pasta) which creates a database, username and password (all the same) which works in php 7.4

<?php 
$usage = 
  "dbusercreate - quickly create a db with user and password with privileges\n".
  'php '.$argv[0].' <db-user-pass> <root-password>'."\n";

if (empty($argv[1])){
  die($usage);
}
if (empty($argv[2])){
  die($usage);
}
$dbuserpass = $argv[1];
$rootpass = $argv[2];
$con=mysqli_connect("localhost","root", $rootpass);

// check con...
if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error()."\n";
  exit(1);
}

// Create database
$sql="CREATE DATABASE $dbuserpass";
echo $sql."\n";
if (mysqli_query($con,$sql) == false){
  echo "Error creating database: " . mysqli_error($con)."\n";
  exit(1);
}

// Create user
$sql='GRANT usage on *.* to ' . $dbuserpass . '@localhost identified by ' . "'" . "$dbuserpass" . "'";
echo "$sql"."\n";
if (mysqli_query($con,$sql) == false){
  echo "Error creating database user: " . mysqli_error($con)."\n";
  exit(1);
}

// Create user permissions
$sql="GRANT all privileges on $dbuserpass.* to $dbuserpass@localhost";
echo "$sql"."\n";
if (mysqli_query($con,$sql) == false){
  echo "Error creating database user: " . mysqli_error($con)."\n";
  exit(1);
}

echo "Database Name: $dbuserpass"."\n";
echo "Database Username: $dbuserpass"."\n";
echo "Database Password: $dbuserpass"."\n";
echo "Database Host: localhost"."\n";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.