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'));
}