简体   繁体   中英

mysql: Using php, how does one catch the error when mysqli_connect() fails?

Yes, this has been asked before. The other answers did not help.

I am new to mysql and php. I am taking a college course and I have a html-php-mysql project in front of me.

I have been coding for 30 years and working with relational databases for 20. So I have some experience. Yes, it is homework, but I have written a necessary bit of simple code that does not work.

I feel the core issue is the class has no textbook(s) and we were instructed to "find all the help you needed on the web". As there are many ways to skin a cat with php, it has been a challenge to find examples that match those of my instructor. Finding deprecated mysql_* examples makes the search more difficult. And most of the code samples assume the queries will always work! And the db connects as well. This is the last bit. My php handles all the other errors I can throw at it.

This snippet is a frankenstein, but I have not gotten this to work. Bear with me and the code.

No doubt there are many things one should do to make this production code, but that is not the goal. I am trying to get toy html files and php scripts to connect to a toy mysql database and produce a near-trivial result. All those considerations are not germane.

I am forcing the connect to fail so my code can gracefully tell the user the system is down and I can log the error.

A php error is thrown as "rootx" is invalid.

"PHP Warning:  mysqli_connect(): (HY000/1045): Access denied for user 'rootx'@'localhost'"

But the error is not caught, "catch" is skipped and the "finally" clause runs.

mysqli_report(MYSQLI_REPORT_STRICT);

was added as this line fixed this identical problem. It did not help.

Might it be how wamp is configured? All I did was install on this windows 10 box as I was directed by the installer.

mysqli_report(MYSQLI_REPORT_STRICT);
try {
    $conn = mysqli_connect( 'localhost', 'rootx', '',  'csc443' );
                          // server,     username, pw, dbname
    }
} catch( Exception $e ) {
    log_action( "dbconnectfail " . $username . " | " . $e->getCode() . " | " . $e->getMessage() );
    header( 'Location: 500.php' );
    return;    
} finally {
    log_action( "dbconnect localhost::csc443" );
}

Remove all try, catch and finally clause. Use below code to make sure what the issue is in connection.

$conn = mysqli_connect( '<server>', '<username>', '<password>',  '<database-name>');
if (!$conn) {
  // whatever processing you want to do upon error in connection
  // like log error or send an email to admin
  // I am just printing the error at the moment.
  echo mysqli_connect_errno() . ":" . mysqli_connect_error();
  exit;
}

Excerpt from Official documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM