I'm trying to connect to a database. If my php fails to connect to that database then it's programmed to create that database. I'm at a little bit of a dilemma, though; because I'm receiving this error: "Error creating database: Can't create database 'my_db'; database exists" So the database is there, but it appears that my php is unable to connect to it.
Here's my code:
<?php
$db = "my_db";
//establish a connection with the server
$connection = mysqli_connect('localhost', 'root', '205360hello');
if(!$connection){
exit("<p>Could not establish a connection :" . mysqli_connect_error() . "</p>");
}
//connect to the database
$dbSelect = mysqli_select_db($db);
if(!$dbSelect){
// Create database
$sql="CREATE DATABASE " . $db;
if (mysqli_query($connection, $sql)) {
} else {
echo "<p>Error creating database: " . mysqli_error($connection) . "</p>";
}
}
?>
Any help would be greatly appreciated!
CREATE DATABASE IF NOT EXISTS my_db;and let the SQL Server handle the processing of this request?