php - How to use try/catch exception in mysql database connect -


i need connect database , don't need show errors. must use try/catch exception.

 class mysqldatabase { private  $connection;  function __construct() {     $this->open_connection(); } public function open_connection() {     $this->connection = mysql_connect(host_name, db_user, db_password);     if (!$this->connection) {         die('not connected: ' . mysql_error());     } else {          $db_selected = mysql_select_db(db_name, $this->connection);         if (!$db_selected) {             die ('can\'t use foo : ' . mysql_error());         }     } }  public function getconnection() {     return $this->connection; } 

}

why wouldn't use pdo

class mysqldatabase  {     private  $connection;      public function __construct()      {        $this->open_connection();     }      public function open_connection()      {         try {             $this->connection = new pdo(                 sprintf('mysql:host=%s;dbname=%s;',                 host_name,db_name),                 db_user,                 db_password,                 array(                    pdo::mysql_attr_init_command => 'set names \'utf8\''             ));             $this->connection->setattribute( pdo::attr_errmode, pdo::errmode_exception);          } catch (pdoexception $e) {              printf('connected problem, code: %s, meassage: %s',$e->getcode(),$e->getmessage());             exit(1);         }      }      public function getconnection()      {         return $this->connection;     } }