I'm following an OOP mysqli course. When connecting to the database, they use the following script:
$db = new mysqli("host", "user", "password", "database");
if ($db->connect_error){
$error = $db->connect_error;
echo("Not connected: " . $error);
}
Later though, they call the database connection file with a try / catch block:
try {
require_once "connection.php";
}catch (Exception $e){
$error = $e->getMessage();
echo $error;
}
Isn't a possible connection error being handled by the connection file immediately after trying to connect? Is the try / catch block basically doing the same thing? Or is the try / catch block looking for a different type of error?
UPDATE: Just to clarify, after reading some of the answers. When I just do this:
try {
$db = new mysqli("host", "user", "password", "database");
}catch (Exception $e){
$error = $e->getMessage();
echo $error;
}
assuming that the database access data is wrong (for example a wrong host), I get a PHP warning but not the error output in the catch block. Shouldn't the catch detect this error?