How to create database in MySQL using PHP? 

The CREATE DATABASE statement is used to create database. (Note: Before creating database you must first connect with database server).Here is an example to create database using PHP:


$server_name = "localhost";

$uname = "username";

$pass= "password";



$connection = new mysqli($server_name, $uname, $pass); // connecting with database server

// Check connection

if ($connection->connect_error)


    die("Connection failed: " . $connection->connect_error);



// Create database


if ($connection->query("CREATE DATABASE first_db") === TRUE)


    echo "Database created successfully";




    echo "Error creating database: " . $connection->error;



$connection->close(); // It will close the connection


Leave Comment