Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Redirect users if there is a database connection error. Re: Redirect users if there is a database connection error.

#126523
__
Participant

**Don’t** disable error reporting. How will you ever discover something is wrong?

Use your `php.ini` file to hide error messages from the user, and write them to an error log instead.

As for your code, `die()` means just that: the script dies. None of your following code will happen. This should show why using `die` is such a terrible way to handle errors: your users get broken pages. Instead, if there’s a problem, do something deliberate:

# the mysql_* functions are deprecated
# and should not be used.
# instead, try mysqli or PDO
$myconnection = new mysqli( ‘host’,’user’,’pass’,’db_name’ );

# DON’T “or die()”!
# calmly check if there was an error.
if ($mysqli->connect_error) {

# redirect to your error page
header( ‘Location: http://example.com/redirect.html’ );

# here, you *really* don’t want any following code to execute,
# so die() is good.
die;
}