PHP MySQL Create Database

The CREATE DATABASE statement is used to create new database.

MySQL CREATE DATABASE Statement Syntax

CREATE DATABASE database_name;

Example

In this example we will create a new database users.

CREATE DATABASE users;

PHP MySQL Create Database Statement

The CREATE DATABASE statement is used to create new database in MySQL.
To create a new database,first of all you need to connect with mysql.

<?php
$connection = mysqli_connect("localhost","username","password");//You need to only specify first three arguments
// Check connection
if (mysqli_connect_errno()){
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "CREATE DATABASE users";
if(mysqli_query($connection,$sql) == TRUE){
   echo "Database Successfully Created.";
}else{
   echo "Error Creating Database: ".mysqli_error($connection);
}
mysqli_close($connection);
?>
Info: When you create a new database you need to pass only first three arguments i.e (servername, username, password).
Note: If you have to use a specific post then pass a blank value for database.
e.g: mysqli_connect(“localhost”,”username”,”password”,””,”port”);

Shubham Kumar

Hey, I am Shubham and i love Blogging, Coding and exploring new things and obviously sharing my experience with you.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *