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.

Query.

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.

 

<?php
//creating connection.
$mycon = mysqli_connect('localhost','root','','');
//testing connection.
if($mycon == true)
{
echo "Successfully connected.";
}
else
{
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";
}
else
{
die("Problem in execution".mysqli_connect_error());
}
$mycon->close();
?>

Output.

Successfully connected. Query successfully executed. Database created