12345678910111213141516171819202122232425262728293031 |
- <?php
-
- /**
- * 数据库pdo连接
- */
- class DB_PDO{
-
- private static $pdo;
- private function __construct(){
-
- }
-
- /**
- * 获取实例化的PDO,单例模式
- * @return PDO
- */
- public static function getInstance($dbConf){
- if(!(self::$pdo instanceof PDO)){
- $dsn ="mysql:host=".$dbConf['host'].";port=".$dbConf['port'].";dbname=".$dbConf['dbName'];
- try {
- self::$pdo = new PDO($dsn,$dbConf['user'], $dbConf['password'], array(PDO::ATTR_PERSISTENT => true,PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")); //保持长连接
- self::$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- } catch (PDOException $e) {
- print "Error:".$e->getMessage()."<br/>";
- die();
- }
- }
- return self::$pdo;
- }
- }
|