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");
}
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:
<?php
# 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;
}
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.
Don't disable error reporting. How will you ever discover something is wrong?
Use your
php.inifile 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 usingdieis such a terrible way to handle errors: your users get broken pages. Instead, if there's a problem, do something deliberate: