blog

Home / DeveloperSection / Blogs / How to create database in MySQL using PHP?

How to create database in MySQL using PHP?

Royce Roy 2245 13-Jun-2016

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:

<?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";
 }
else
 {
    echo "Error creating database: " . $connection->error;
 }
$connection->close(); // It will close the connection
?>

Updated 15-Mar-2018

Leave Comment

Comments

Liked By