note 118361 added to pdo.construct

From: Date: Mon, 23 Nov 2015 05:37:59 +0000
Subject: note 118361 added to pdo.construct
Groups: php.notes 
Request: Send a blank email to php-notes+get-204208@lists.php.net to get a copy of this message
You will get a fatal error if you don't catch the exception threw by PDO when it fails to connect to the database server like this. Fatal error: in xxx.php on line xx This error neither can be handled by error handlers nor can it be erased by the @ sign, which can make your script uncontrollable. <?php $db = new pdo('mysql:host=127.0.0.1;port=3306;dbname=mysql;charset=utf8','user','password',array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, )); ?> You should always try ... catch ... <?php try{ $db = new pdo('mysql:host=127.0.0.1;port=3306;dbname=mysql;charset=utf8','user','password',array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, )); }catch(PDOException $pe){ echo $pe->getMessage(); } ?> ---- Server IP: 72.52.91.14 Probable Submitter: 118.117.77.70 ---- Manual Page -- http://php.net/manual/en/pdo.construct.php Edit -- https://master.php.net/note/edit/118361 Del: integrated -- https://master.php.net/note/delete/118361/integrated Del: useless -- https://master.php.net/note/delete/118361/useless Del: bad code -- https://master.php.net/note/delete/118361/bad+code Del: spam -- https://master.php.net/note/delete/118361/spam Del: non-english -- https://master.php.net/note/delete/118361/non-english Del: in docs -- https://master.php.net/note/delete/118361/in+docs Del: other reasons-- https://master.php.net/note/delete/118361 Reject -- https://master.php.net/note/reject/118361 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#204208) next »