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

How to create database in MySQL using PHP?


PHP Database  PHP  MYSQL 
Ratings:
0 Comment(s)
 191  View(s)
Rate this:

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:

<?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

?>


Don't want to miss updates? Please click the below button!

Follow MindStick