Exemplo n.º 1
0
 public static function get()
 {
     if (is_null(self::$db)) {
         self::$db = new PDO(self::$dsn, self::$user, self::$pass);
     }
     //返回连接
     self::$db->query('set names utf8');
     return self::$db;
 }
Exemplo n.º 2
0
 public static function get()
 {
     // Connect if not already connected
     if (is_null(self::$db)) {
         self::$db = new PDO(self::$dsn, self::$user, self::$pass, self::$driverOpts);
     }
     // Return the connection
     return self::$db;
 }
Exemplo n.º 3
0
 public static function get()
 {
     // Connect if not already connected
     if (is_null(self::$db)) {
         try {
             // sprintf to get around the fact you can't create teh dsn dynamically by concatting vars.
             self::$db = new PDO(sprintf("mysql:host=%s;dbname=%s;charset=utf8", DB_SERVER, DB_NAME), DB_USER, DB_PASSWORD, self::$driverOpts);
             // set up PDO to throw exceptions so I can use try and catch blocks in the code http://www.kitebird.com/articles/php-pdo.html#TOC_5
             self::$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
         } catch (PDOException $e) {
             echo $e->getMessage();
         }
     }
     // Return the connection
     return self::$db;
 }
Exemplo n.º 4
0
Arquivo: Db.php Projeto: bas2/classes
 public static function get($dbname)
 {
     // Connect if not already connected
     if (is_null(self::$db)) {
         #### GET DATABASE CREDENTIALS ####
         if (!file_exists($dbname)) {
             die("No database credentials file! for: {$dbname}");
         }
         require_once $dbname;
         # Above web root
         $dbname = substr($dbname, strrpos($dbname, '/') + 1);
         $dbname = str_replace('.php', '', $dbname);
         # The name of the database is the file name minus its .php extension.
         self::$dbname = $dbname;
         self::$user = $user;
         self::$pass = $pass;
         ##################################
         self::$db = new PDO(self::$dsn . self::$dbname, self::$user, self::$pass, self::$driverOpts);
     }
     // End if.
     return self::$db;
     # Return the connection.
 }