Create new mysql database using php

Creating new mysql database with php script.

In this tutorial we are going to create a new database with php script.


For creating new mysql database we use following query : create database <database_name>

In my case i am going to create a new database named as new_db.


//creating connection.
$mycon = mysqli_connect('localhost','root','','');
//testing connection.
if($mycon == true)
echo "Successfully connected.";
die('Problem with connection'.mysqli_connect_error());
//Create database query.
$query = "create database new_db";
$res = mysqli_query($mycon,$query);
//Testing query.
if($res == true)
echo "Query successfully executed. Database created";
die("Problem in execution".mysqli_connect_error());


Successfully connected. Query successfully executed. Database created