php - How to display my own errors instead of default errors using pdo connection? -
i want display own errors instead of default errors using pdo connection.the error can categorized as
1. if database doesn't exist
2. access denied
3. host name invalid
a simple code of pdo connection is
<?php $host = 'localhost'; $dbname = 'test'; $user = 'root'; $password = ''; try { $db = new pdo("mysql:host=$host;dbname=$dbname", $user, $password); } catch (pdoexception $e) { echo $e->getmessage(); } ?> now, can use if statements in catch section? if so, how can achieve output? don't want use getmessage() function.
thanks
first, use driver_options param of constructor make exceptions being thrown on errors:
$db = new pdo("mysql:host=$host;dbname=$dbname", $user, $password, array( pdo::attr_errmode => pdo::errmode_exception )); then in catch block you'll have analyze code property of exception:
switch($e->getcode()) { .... } you'll find list of codes , meaning in mysql manual
Comments
Post a Comment