Creating a MySQL database from PHP with username, password and all privileges

I have a php file that connects to a mysql database.

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

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

DB name, DB user, Password and all privileges

Is there any way to implement this?

+3
source share
4 answers

You can do it as follows:

$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 

}
+1
source

. MySQL PHP, /. , , , /, // ?

+1

See MySQL Documentation for CREATE USER and GRANT .

0
source
$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{
}
0
source

Source: https://habr.com/ru/post/1777201/


All Articles