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.

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #43052
    ajnoguerra
    Participant

    Hi guys, I’ve been playing with my php scripts for some time now, I just need a little help on how to redirect my visitors to another page that says a much clear message that the current website has encountered some database connection error or is under construction maybe. I have disabled error reporting which is – error_reporting(0);

    A really simple markup will be much appreciated. Thank you.

    error_reporting(0);

    $myconnection = mysql_connect(“localhost”,”root”,””) or die($myconnection);
    $dbconnection = mysql_select_db(“db”) or die($dbconnection);

    if (die($myconnection) === true){
    header(“Location: redirect.html”);
    }

    #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;
    }

Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘Back End’ is closed to new topics and replies.