0

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.

dmikester1
  • 1,374
  • 11
  • 55
  • 113
  • leave out the "db" from the connection, you dont need it, and why are you trying to connect twice? your catch is for errors, you creation of db should either be in the try or a new try catch block –  Apr 19 '19 at 04:11

2 Answers2

2

Your problem is that mysqli::__construct only generates a warning if it can't connect to the database, and you can't directly catch a warning. Now you can workaround that (for example, see this question) but it's probably simpler to do something like this:

$con = new mysqli("host", "user", "pass");
if ($con->connect_errno) {
    printf("Connect failed: %s\n", $con->connect_error);
    exit();
}
if (!$con->select_db('db')) {
    echo "Couldn't select database: " . $con->error;
    if (!$con->query("CREATE DATABASE IF NOT EXISTS db;")) {
        echo "Couldn't create database: " . $con->error;
    }
    $con->select_db('db');
}
Nick
  • 138,499
  • 22
  • 57
  • 95
  • @YourCommonSense thanks for sharing that, I wasn't aware of that option. You should post it as an answer, then I can delete this. – Nick Apr 19 '19 at 07:33
  • I tried this code and it does create the DB, but it does not get any further in the script. It outputs this: `Couldn't select database db: Unknown database 'db'No database selected` – dmikester1 Apr 19 '19 at 18:34
  • @dmikester1 sorry about that I forgot the code to select the db once it had been created. I've edited that in now. You should take a look at the link YourCommonSense shared in the first comment, it looks like it could make your original code work. – Nick Apr 20 '19 at 01:32
  • Perfect! Looks like adding the line from YourCommonSense's link worked as well. But this is a working complete answer. Thanks! – dmikester1 Apr 20 '19 at 03:06
  • @dmikester1 glad it's working. I love the fact that on this site you can learn stuff by answering question as well as asking them! :-) – Nick Apr 20 '19 at 03:09
0
    //check if the database exist - PHP
    $cons = '';
    $cons = new mysqli("host", "user", "pass");
    $okk = mysqli_query($cons, "SHOW DATABASES LIKE 'db'");
    $okkno = mysqli_num_rows($okk);
    
    if($okkno <=0){
    $fanya = mysqli_query($cons, "CREATE DATABASE IF NOT EXISTS db;");
    echo "Database created successifully";
    }
  • Can you explain what this answer brings to a 2 year old question with an already accepted answer? At best it appears to attempt to answer the literal question in the title, but doesn’t take into account the back and forth comments that probe the underlying problem – Chris Haas Dec 18 '21 at 12:51