How to check if database exists in SQL server in PHP

CREATE DATABASE IF NOT EXISTS DBName;
// Create connection
$servername = "localhost";
$username = "CMS_user";
$password = "CMS_pass";

// Create connection
$conn = mysqli_connect($servername, $username, $password);


// Check connection
if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
}

// Create database
$sql = "CREATE DATABASE IF NOT EXISTS my_CMS";
if (mysqli_query($conn, $sql)) {
  echo "Database created successfully";
} else {
  echo "Error creating database: " . mysqli_error($conn);
}

Leave a Reply