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; }
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; }
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; }
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. }