0

Using MySQLi connection method I am trying to redirect page instead of printing out the error message

<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', 'password');
define('DB_NAME', 'demo');
 
$mysqli = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
 
// Check connection
if($mysqli === false){
    die("ERROR: Could not connect. " . $mysqli->connect_error);
    header('Location: http://www.example.com/'); 
}
?>

trying to test this out I change the localhost to localhostszzz but I am getting error message of

Warning: mysqli::__construct(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /../../page.php on line 8

Instead of re-directing to http://www.example.com/, how can I fix this?

halfer
  • 19,824
  • 17
  • 99
  • 186
Behseini
  • 6,066
  • 23
  • 78
  • 125

3 Answers3

1

You cannot redirect on warning. Also, you shouldn't redirect when a connection cannot be established. You should let the page throw a fatal error that will result in a nice error page for the user.

But if you want to handle warnings, you need a custom error handler. An error handler is something that takes all unhandled exceptions, errors, and warnings and processes them. The built-in error handler can either display the error in the browser or log to a file. You would have to create your own error handler that is capable of handling warnings as well as all other types of errors.

I am not going to show you how to build a custom error handler as this is a very broad topic. However, I will make a better suggestion. Your users should never see the PHP internal error messages. Your users should see a friendly 500 page explaining that there was a problem in the software and that the issue was logged in a file for developers to take a look into it. You should also give users an option to contact you in case of emergencies.

When it comes to mysqli error handling, you should always enable error reporting and never check manually for errors. The connection is opened with the following two lines:

// enable error reporting
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
// create the object
$mysqli = new mysqli('localhost', 'inet', '5432', 'test');

Don't check for errors manually. However, your warning does not come from mysqli, it comes from the network API in PHP. The setting above won't have an impact on that warning. So fix the error, enable error logging, and implement a nice 500 error page for users. Don't add any special handling for warnings such as this.


In case you are still interested in a quick fix for this particular warning, you can try the following. I strongly do not recommend using it, it is only for demonstration purposes.

Silence warnings with error suppression operator @. Enable mysqli error reporting which will also trigger the same exception. Catch the exception.

try {
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    @$mysqli = new mysqli('localhost1', 'inet', '5432', 'test');
} catch (mysqli_sql_exception $exception) {
    exit(header('Location: error500.html'));
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
-1

OK, This is what I was looking for and is working for me mow

  try{
      $conn = new mysqli($servername, $username, $password, $dbname);
  }
  catch(Exception $e){
    header('Location: 500.php');
    die();
}
Behseini
  • 6,066
  • 23
  • 78
  • 125
  • Location: 500.php makes no sense. it will tell a client that current page's location has been changed to 500.php and it should be requested and indexed instead – Your Common Sense Jan 25 '23 at 10:28
-3

You just need to edit the if condition. For more help, you can visit w3schools

<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', 'password');
define('DB_NAME', 'demo');

$mysqli = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);

if ($mysqli -> connect_errno) {
  echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
    header('Location: http://www.example.com/');
}
?>
Hardi
  • 26
  • 2
  • 2
    You can't send content before the header data or the redirect won't work. – bassxzero Jan 05 '21 at 02:08
  • Thanks for reply but still same thing is happening – Behseini Jan 05 '21 at 02:10
  • @bassxzero it should possible to send content with "echo" before the redirect. But if you use the "die()" function it won`t work – Hardi Jan 05 '21 at 02:31
  • @Behseini if you want to supress the warning you can make use of the function "error_reporting". With this function you should be able to supress the warning message – Hardi Jan 05 '21 at 02:33
  • You need to stop manually checking for errors. Please read: [Should we ever check for mysqli_connect() errors manually?](https://stackoverflow.com/q/58808332/1839439) and [Should I manually check for errors when calling “mysqli_stmt_prepare”?](https://stackoverflow.com/q/62216426/1839439) – Dharman Jan 05 '21 at 13:46
  • @Hardi where should such content be displayed if the browser is told to request another URL? – Nico Haase Jan 05 '21 at 13:49