Users Pricing

blog

home / developersection / blogs / how to create database in mysql using php?

How to create database in MySQL using PHP?

Royce Roy 2695 13 Jun 2016 Updated 15 Mar 2018

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

Royce Roy

Other


1 Comments