1

Hey guys I want to be able to run queries to build up my database if mysqli returns error constant of 'ER_BAD_DB_ERROR'/1049. Here is my code. If the error is caught I don't want mysqli to display the error but rather just built the database.

What happens it that the unknown database error is display, error is caught in the if() statement and the database is built.

I realize mysqli if displaying the error because of this:

$connection = mysqli_connect($db_server, $db_user, $db_pwd, $dbname);

How do I just build the database in place of displaying the error if that exact error that I want is caught.

$db_server = "localhost";
$db_user = "user";
$db_pwd = "";
$dbname = "name";

#connect to database
$connection = mysqli_connect($db_server, $db_user, $db_pwd, $dbname);

#try to catch database not exist error
if ('ER_BAD_DB_ERROR') {
    # if true build database and connect again
    build_db();
    $connection = mysqli_connect($db_server, $db_user, $db_pwd, $dbname);
} else {
    #close connection with other errors encounter
    die("database connection failed". mysqli_connect_error());
}

I'm not sure if I'm following best practices here. So, I'm wholeheartedly open to suggestions and feedback. Thanks Guys.

2

1 Answer 1

1

You are looking for

Example:

$link = mysqli_connect('localhost', 'user', 'password', 'my_db');

// check if the connect call returned a database object
if (!$link) {
    // check the particular error number why the connect failed
    if (mysqli_connect_errno() === ER_BAD_DB_ERROR) {
        // the database does not exist, so build it now
        build_db();
    } else {
        // something else caused the connect to fail
    }
}

This assumes the constant ER_BAD_DB_ERROR exists. I haven't checked if it does. If it doesn't exist, you will need to define it with a value corresponding to the correct error code returned by mysqli_connect_errno.

You can also use mysqli_connect_error() to get a textual representation of the error and compare that instead of just the error code.

Quoting the PHP Manual on where to find the error codes:

Client error message numbers are listed in the MySQL errmsg.h header file, server error message numbers are listed in mysqld_error.h. In the MySQL source distribution you can find a complete list of error messages and error numbers in the file Docs/mysqld_error.txt.

Note that this will still display a message about the missing database. You can either introduce a custom error handler for this particular error or just put an error suppression operator in front of the mysqli_connect call, e.g. change it to @mysqli_connect.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. I got it working by using the error suppression operator, but realize that it suppresses all other errors from displaying. In situation that I miss the user and password it just goes and still build the database if it doesn't exists. Is there a way that I can suppress only this error(ER_BAD_DB_ERROR) from displaying?
@NathanSiafa the code above should already only call build_db() when this particular error ER_BAD_DB_ERROR occurs. Any other error would go to the else block. You can also use mysqli_connect_error() to get a textual representation of the error and compare that instead of just the error code.
I'm so sorry. I realize I had another connection in build_db() which was overwriting the connection I had, I have fix it and it now works perfectly. Thanks so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.