0

I am less familiar with mysqli, and I write Procedure style, not Object orientated style. I know of some of the benefits of object orientated style but it trips my mind so I am slow to adjust with it. My policy is, if I cannot support it, I don't write it.

So... I have a simple function which contains the following:

mysqli_query( $DB, "CREATE TABLE $TABLE ( $SCHEMHA )" );

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

The query works when creating the table, so long as I don't have a bug in my schema. If my schema fails, my conditional statement does not trap the failure (thus the connect failed message never gets called).

Where am I going wrong?

My goal is to be able to exit/stop/end on selective errors. So for example, if a table already exists (errno 1050), I am comfortable ignoring that error, however if there is an error 1072 (Key column 'whatever' doesn't exist in table) I would like to stop processing.

All help appreciated...

3
  • No error should ever be ignored... Commented Feb 26, 2016 at 12:34
  • Thanks for the comment... but that does not help me with my question... Commented Feb 26, 2016 at 12:37
  • Thanks Chris - I want to trap the error number, not the error message. When I have the error number, I can decide which errors I want to ignore (some tables in my test database already exist so hence I only want to stop setup during certain conditions - or ignore during certain conditions depending on point of view). Commented Feb 26, 2016 at 12:59

3 Answers 3

2
  1. You'll want to examine the return value of mysqli_query to know whether the query succeeded or failed.
  2. You can get the exact error code using mysqli_errno.
  3. The _connect_ functions you're using are irrelevant in this context, they give you information about errors which happened during a connection attempt.
if (!mysqli_query(..)) {
    switch (mysqli_errno($DB)) {
        case 1072:
            ...
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Fred, I swear this autocorrect is driving me insane sometimes.
Thank you Good Sir! I raise my hat, bow my head in appreciation =)
2

The mysqli_connect_errno returns the error from the connection. Your error is resulting in the query though so use mysqli_error.

if (mysqli_error($DB)) {
    printf("Errormessage: %s\n", mysqli_error($link));
}

If you just want to check the error code use http://php.net/manual/en/mysqli.errno.php.

printf("Errorcode: %d\n", mysqli_errno($DB));

To check for specific codes you could do:

if(in_array(mysqli_errno($DB), array(1072, other codes)) {
    exit('Bad Code');
} else {
    echo 'Still processing...';
}

Comments

0

So for example, if a table already exists (errno 1050), I am comfortable ignoring that error,

CREATE TABLE IF NOT EXISTS $TABLE ...

Comments

Your Answer

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