Create Database in PHP If It Does Not Exist

This query will create a SQL database if it does not exist. If database already exists then this will return true.

Query-

CREATE DATABASE IF NOT EXISTS database_name;

Example-

<?php

$db = new mysqli("localhost","root","");
$create_db = "CREATE DATABASE IF NOT EXISTS ifsclist";
if($db->connect_error)
{
	die("connection error");
}

$response = $db->query($create_db);
if($response)
{
	echo "When created or Already exists";
}
else{
	echo "Something wrong!";
}

?>

Leave a Comment