-1
People would like to treat the error that occurs if the Mysql server is not running, I want only one message to appear so I made the following code:
function mensagemErro () {
throw new \Exception("Error connecting to database");
}
function connect() {
try {
$pdo = new \PDO("mysql:host=localhost;dbname=DB;charset=utf8", 'root', 'root');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
return $pdo;
} catch (Exception $e) {
mensagemErro();
echo $e->getMessage();
}
}
But the custom message is not showing, only the Fatal Error: < path of the.php file > on line 11 And I don’t want the file name and the error line to appear, only the message.
What is the complete error message? A
Fatal Error
is not an exception.– Woss
Has a native function of
PHP
that can meet your need simply, she callsPDO::errorInfo
which has a function similar tomysqli_error
. Link:http://php.net/manual/en/pdo.errorinfo.php– Thiago Drulla
Yes, Fatal Error is not caught by catch because it is not an exception. I will test with PDO::errorInfo, thanks for the tip.
– Beto