I am using PHP and mysqli and trying to create the DB if it does not exist in my dbLogin file. Seems like it should be a fairly simple thing to do. It seems to be getting hung up on the first creation attempt with an error and won't move past or catch the error.
Here is my very simple code:
<?php
$con = '';
try {
$con = new mysqli("host", "user", "pass", "db");
}
catch(Exception $e) {
echo "Error: $e<br />";
$con = new mysqli("host", "user", "pass");
$con->query("CREATE DATABASE IF NOT EXISTS db;");
}
/* check connection */
if ($con->connect_errno) {
printf("Connect failed: %s\n", $con->connect_error);
exit();
}
?>
Now, when I run the code above, all I get is the following error:
Warning: mysqli::__construct(): (HY000/1049): Unknown database 'db' in xyz\DBLogin.php on line 6 Connect failed: Unknown database 'db'
Line 6 is the line inside the try
block. It does not create the database and does not move past the error.