Create Database in PHP If It Does Not Exist

Date Published: 22/03/2021 Published By: JaiSchool

This query will create a SQL database if it does not exist. If database already exists then it is going to return false.

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!";
}

?>

Publish A Comment

Leave a Reply

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