Jump to content

mysqli - how can I let page continue to load if there's an error?


Recommended Posts

Hi Everyone,

I'd like my webpage to continue to load if there's an error with a mysqli connection. Right now, my page just stops loading at the point of the error. I've tried catching the error (source code below) or ignoring the error, but it still just stops any more php running on the page.

 

$con = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname); 

if (mysqli_connect_errno()) {  // Check connection - this doesn't seem to work. page just stops loading if connection error. 
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

 

So for example, if I change $dbuser to be incorrect, the error message never gets echoed and the page stops loading any further.

I'd love to be able to catch an error (like if the database server is temporarily down) and have the page fully load but do so gracefully or informatively. Not sure what I'm doing wrong, this code example seems to be very standard across every website I've seen regarding mysqli connections.

Thanks in advance for the help!

 

you don't want to give hackers useful feedback as to if they were able to cause any specific problem on a site. you also don't want to give legitimate visitors information that they don't need to know and cannot do anything about.

database errors are fatal for a page that is database dependent. all you need to let the visitor/hacker know is that the current page isn't working, and you need to log the actual errors. you never want to output the raw errors on a live/public site.

modern php (8+) uses exceptions for database errors by default. when using exceptions, no discrete error handling logic in your code will ever get executed upon an error and should be removed, because execution transfers to the nearest correct type of exception try/catch block in your code, or to php if there is no correct type of exception handling in your code. this is the reason why you are not 'seeing' anything when you deliberately trigger a connection error. execution is going to php's uncaught exception handling, where php is using its error related settings (error_reporting, display_errors, and log_errors) to control what happens with the raw error information. php's error_reporting should always be set to E_ALL. on a live/public site, php's display_errors should be set to OFF and log_errors should be set to ON. this will cause all php errors, which includes uncaught exceptions, to be logged. if your server is setup this way, php should generate a http 500 error response. you can setup a custom http 500 page with any information or text on it that you want, such as - this page is not working, the site owner has been notified. please come back later to try again...

if you need to handle uncaught exceptions more specifically, such as sending an email or sms when they occur, you can register your own uncaught exception handler - see https://www.php.net/manual/en/function.set-exception-handler.php

as to your application code. you should only catch and handle database exceptions for user recoverable errors, such as when inserting/updating duplicate or out of range user submitted data. for all other query errors and all other type of queries, you should do nothing in your application code and let php, or if you have defined your own uncaught exception handler, catch and handle exceptions.

  • Great Answer 1

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.