Using MySQLi connection method I am trying to redirect page instead of printing out the error message
JavaScript
x
<?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?
Advertisement
Answer
OK, This is what I was looking for and is working for me mow
JavaScript
try{
$conn = new mysqli($servername, $username, $password, $dbname);
}
catch(Exception $e){
header('Location: 500.php');
die();
}