I want to use try{} catch{}
in PDO and display custom errors in catch, but errors sent automatically to the user and I do not control them.
For example, this is my code for connecting to the database
class cn{
public static function connect( $dbName=NULL ){
try{
if( $dbName != NULL )
$dbName = 'dbname='.$dbName;
$con = new PDO(
'mysql:host=localhost;'.$dbName,
'myuser',
'mypass',
[
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]
);
}catch(PDOException $e){
if($e->getCode() == 1049)
echo 'not find';
elseif($e->getCode() == x)
echo 'ABC';
elseif($e->getCode() == y)
echo 'DEF';
.
.
.
else
echo 'XYZ';
}
return $con;
}
}
But the errors are displayed as shown in the image below.