コード例 #1
0
ファイル: db.php プロジェクト: Richard-Whalley/jocular-duck
 private function __construct()
 {
     try {
         // Assign PDO object to db variable using super secure password
         self::$db = new PDO('mysql:host=localhost;dbname=guestbook', 'homestead', 'secret');
         self::$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
     } catch (PDOException $e) {
         // Return connection errors - this is bad practice; should write to file
         echo "Connection Error: " . $e->getMessage();
     }
 }
コード例 #2
0
 /**
  * Get a new database connection returns a pdo object.
  * @return PDO 
  */
 public static function getInstance()
 {
     if (self::$db === NULL) {
         try {
             self::$db = new PDO('mysql:dbname=' . DB . ';' . DB_HOST, DB_USERNAME, DB_PASSWORD);
             self::$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
         } catch (Exception $e) {
             throw new Exception($e->getMessage(), $e->getCode());
         }
     }
     return self::$db;
 }