国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

PHP單例模式編寫的PDO類的程序

2024-05-04 21:49:46
字體:
來源:轉載
供稿:網友

下面的代碼是用此前一個名為MyPDO的類改寫的,引入了單例模式來保證在全局調用中不會重復實例化這個類,降低系統資源的浪費,用php大部分操作都是和各種數據庫打交道,包括mysql,redis,memcache等各種關系型和非關系型數據庫,所以一個應用中會存在大量連接數據庫的操作,如果不用單例模式,那每次都要new操作,但是每次new都會消耗大量的內存資源和系統資源,而且每次打開和關閉數據庫連接都是對數據庫的一種極大考驗和浪費,代碼如下:

  1. <?php 
  2.  
  3. class MyPDO 
  4.     protected static $_instance = null; 
  5.     protected $dbName = ''
  6.     protected $dsn
  7.     protected $dbh
  8.      
  9.     /** 
  10.      * 構造 
  11.      *  
  12.      * @return MyPDO 
  13.      */ 
  14.     private function __construct($dbHost$dbUser$dbPasswd$dbName$dbCharset
  15.     { 
  16.         try { 
  17.             $this->dsn = 'mysql:host='.$dbHost.';dbname='.$dbName
  18.             $this->dbh = new PDO($this->dsn, $dbUser$dbPasswd); 
  19.             $this->dbh->exec('SET character_set_connection='.$dbCharset.', character_set_results='.$dbCharset.', character_set_client=binary'); 
  20.         } catch (PDOException $e) { 
  21.             $this->outputError($e->getMessage()); 
  22.         } 
  23.     } 
  24.      
  25.     /** 
  26.      * 防止克隆 
  27.      *  
  28.      */ 
  29.     private function __clone() {} 
  30.      
  31.     /** 
  32.      * Singleton instance 
  33.      *  
  34.      * @return Object 
  35.      */ 
  36.     public static function getInstance($dbHost$dbUser$dbPasswd$dbName$dbCharset
  37.     { 
  38.         if (self::$_instance === null) { 
  39.             self::$_instance = new self($dbHost$dbUser$dbPasswd$dbName$dbCharset); 
  40.         } 
  41.         return self::$_instance
  42.     } 
  43.      
  44.     /** 
  45.      * Query 查詢 
  46.      * 
  47.      * @param String $strSql SQL語句 
  48.      * @param String $queryMode 查詢方式(All or Row) 
  49.      * @param Boolean $debug 
  50.      * @return Array 
  51.      */ 
  52.     public function query($strSql$queryMode = 'All'$debug = false) 
  53.     { 
  54.         if ($debug === true) $this->debug($strSql); 
  55.         $recordset = $this->dbh->query($strSql); 
  56.         $this->getPDOError(); 
  57.         if ($recordset) { 
  58.             $recordset->setFetchMode(PDO::FETCH_ASSOC); 
  59.             if ($queryMode == 'All') { 
  60.                 $result = $recordset->fetchAll(); 
  61.             } elseif ($queryMode == 'Row') { 
  62.                 $result = $recordset->fetch(); 
  63.             } 
  64.         } else { 
  65.             $result = null; 
  66.         } 
  67.         return $result
  68.     } 
  69.      
  70.     /** 
  71.      * Update 更新 
  72.      * 
  73.      * @param String $table 表名 
  74.      * @param Array $arrayDataValue 字段與值 
  75.      * @param String $where 條件 
  76.      * @param Boolean $debug 
  77.      * @return Int 
  78.      */ 
  79.     public function update($table$arrayDataValue$where = ''$debug = false) 
  80.     { 
  81.         $this->checkFields($table$arrayDataValue); 
  82.         if ($where) { 
  83.             $strSql = ''
  84.             foreach ($arrayDataValue as $key => $value) { 
  85.                 $strSql .= ", `$key`='$value'"
  86.             } 
  87.             $strSql = substr($strSql, 1); 
  88.             $strSql = "UPDATE `$table` SET $strSql WHERE $where"
  89.         } else { 
  90.             $strSql = "REPLACE INTO `$table` (`".implode('`,`'array_keys($arrayDataValue))."`) VALUES ('".implode("','"$arrayDataValue)."')"
  91.         } 
  92.         if ($debug === true) $this->debug($strSql); 
  93.         $result = $this->dbh->exec($strSql); 
  94.         $this->getPDOError(); 
  95.         return $result
  96.     } 
  97.      
  98.     /** 
  99.      * Insert 插入 
  100.      * 
  101.      * @param String $table 表名 
  102.      * @param Array $arrayDataValue 字段與值 
  103.      * @param Boolean $debug 
  104.      * @return Int 
  105.      */ 
  106.     public function insert($table$arrayDataValue$debug = false) 
  107.     { 
  108.         $this->checkFields($table$arrayDataValue); 
  109.         $strSql = "INSERT INTO `$table` (`".implode('`,`'array_keys($arrayDataValue))."`) VALUES ('".implode("','"$arrayDataValue)."')"
  110.         if ($debug === true) $this->debug($strSql); 
  111.         $result = $this->dbh->exec($strSql); 
  112.         $this->getPDOError(); 
  113.         return $result
  114.     } 
  115.      
  116.     /** 
  117.      * Replace 覆蓋方式插入 
  118.      * 
  119.      * @param String $table 表名 
  120.      * @param Array $arrayDataValue 字段與值 
  121.      * @param Boolean $debug 
  122.      * @return Int 
  123.      */ 
  124.     public function replace($table$arrayDataValue$debug = false) 
  125.     { 
  126.         $this->checkFields($table$arrayDataValue); 
  127.         $strSql = "REPLACE INTO `$table`(`".implode('`,`'array_keys($arrayDataValue))."`) VALUES ('".implode("','"$arrayDataValue)."')"
  128.         if ($debug === true) $this->debug($strSql); 
  129.         $result = $this->dbh->exec($strSql); 
  130.         $this->getPDOError(); 
  131.         return $result
  132.     } 
  133.      
  134.     /** 
  135.      * Delete 刪除 
  136.      * 
  137.      * @param String $table 表名 
  138.      * @param String $where 條件 
  139.      * @param Boolean $debug 
  140.      * @return Int 
  141.      */ 
  142.     public function delete($table$where = ''$debug = false) 
  143.     { 
  144.         if ($where == '') { 
  145.             $this->outputError("'WHERE' is Null"); 
  146.         } else { 
  147.             $strSql = "DELETE FROM `$table` WHERE $where"
  148.             if ($debug === true) $this->debug($strSql); 
  149.             $result = $this->dbh->exec($strSql); 
  150.             $this->getPDOError(); 
  151.             return $result
  152.         } 
  153.     } 
  154.      
  155.     /** 
  156.      * execSql 執行SQL語句 
  157.      * 
  158.      * @param String $strSql 
  159.      * @param Boolean $debug 
  160.      * @return Int 
  161.      */ 
  162.     public function execSql($strSql$debug = false) 
  163.     { 
  164.         if ($debug === true) $this->debug($strSql); 
  165.         $result = $this->dbh->exec($strSql); 
  166.         $this->getPDOError(); 
  167.         return $result
  168.     } 
  169.      
  170.     /** 
  171.      * 獲取字段最大值 
  172.      *  
  173.      * @param string $table 表名 
  174.      * @param string $field_name 字段名 
  175.      * @param string $where 條件 
  176.      */ 
  177.     public function getMaxValue($table$field_name$where = ''$debug = false) 
  178.     { 
  179.         $strSql = "SELECT MAX(".$field_name.") AS MAX_VALUE FROM $table"
  180.         if ($where != ''$strSql .= " WHERE $where"
  181.         if ($debug === true) $this->debug($strSql); 
  182.         $arrTemp = $this->query($strSql'Row'); 
  183.         $maxValue = $arrTemp["MAX_VALUE"]; 
  184.         if ($maxValue == "" || $maxValue == null) { 
  185.             $maxValue = 0; 
  186.         } 
  187.         return $maxValue
  188.     } 
  189.      
  190.     /** 
  191.      * 獲取指定列的數量 
  192.      *  
  193.      * @param string $table 
  194.      * @param string $field_name 
  195.      * @param string $where 
  196.      * @param bool $debug 
  197.      * @return int 
  198.      */ 
  199.     public function getCount($table$field_name$where = ''$debug = false) 
  200.     { 
  201.         $strSql = "SELECT COUNT($field_name) AS NUM FROM $table"
  202.         if ($where != ''$strSql .= " WHERE $where"
  203.         if ($debug === true) $this->debug($strSql); 
  204.         $arrTemp = $this->query($strSql'Row'); 
  205.         return $arrTemp['NUM']; 
  206.     } 
  207.      
  208.     /** 
  209.      * 獲取表引擎 
  210.      *  
  211.      * @param String $dbName 庫名 
  212.      * @param String $tableName 表名 
  213.      * @param Boolean $debug 
  214.      * @return String 
  215.      */ 
  216.     public function getTableEngine($dbName$tableName
  217.     { 
  218.         $strSql = "SHOW TABLE STATUS FROM $dbName WHERE Name='".$tableName."'"
  219.         $arrayTableInfo = $this->query($strSql); 
  220.         $this->getPDOError(); 
  221.         return $arrayTableInfo[0]['Engine']; 
  222.     } 
  223.      
  224.     /** 
  225.      * beginTransaction 事務開始 
  226.      */ 
  227.     private function beginTransaction() 
  228.     { 
  229.         $this->dbh->beginTransaction(); 
  230.     } 
  231.      
  232.     /** 
  233.      * commit 事務提交 
  234.      */ 
  235.     private function commit() 
  236.     { 
  237.         $this->dbh->commit(); 
  238.     } 
  239.      
  240.     /** 
  241.      * rollback 事務回滾 
  242.      */ 
  243.     private function rollback() 
  244.     { 
  245.         $this->dbh->rollback(); 
  246.     } 
  247.      
  248.     /** 
  249.      * transaction 通過事務處理多條SQL語句 
  250.      * 調用前需通過getTableEngine判斷表引擎是否支持事務 
  251.      * 
  252.      * @param array $arraySql 
  253.      * @return Boolean 
  254.      */ 
  255.     public function execTransaction($arraySql
  256.     { 
  257.         $retval = 1; 
  258.         $this->beginTransaction(); 
  259.         foreach ($arraySql as $strSql) { 
  260.             if ($this->execSql($strSql) == 0) $retval = 0; 
  261.         } 
  262.         if ($retval == 0) { 
  263.             $this->rollback(); 
  264.             return false; 
  265.         } else { 
  266.             $this->commit(); 
  267.             return true; 
  268.         } 
  269.     } 
  270.  
  271.     /** 
  272.      * checkFields 檢查指定字段是否在指定數據表中存在 
  273.      * 
  274.      * @param String $table 
  275.      * @param array $arrayField 
  276.      */ 
  277.     private function checkFields($table$arrayFields
  278.     { 
  279.         $fields = $this->getFields($table); 
  280.         foreach ($arrayFields as $key => $value) { 
  281.             if (!in_array($key$fields)) { 
  282.                 $this->outputError("Unknown column `$key` in field list."); 
  283.             } 
  284.         } 
  285.     } 
  286.      
  287.     /** 
  288.      * getFields 獲取指定數據表中的全部字段名 
  289.      * 
  290.      * @param String $table 表名 
  291.      * @return array 
  292.      */ 
  293.     private function getFields($table
  294.     { 
  295.         $fields = array(); 
  296.         $recordset = $this->dbh->query("SHOW COLUMNS FROM $table"); 
  297.         $this->getPDOError(); 
  298.         $recordset->setFetchMode(PDO::FETCH_ASSOC); 
  299.         $result = $recordset->fetchAll(); 
  300.         foreach ($result as $rows) { 
  301.             $fields[] = $rows['Field']; 
  302.         } 
  303.         return $fields
  304.     } 
  305.      
  306.     /** 
  307.      * getPDOError 捕獲PDO錯誤信息 
  308.      */ 
  309.     private function getPDOError() 
  310.     { 
  311.         if ($this->dbh->errorCode() != '00000') { 
  312.             $arrayError = $this->dbh->errorInfo(); 
  313.             $this->outputError($arrayError[2]); 
  314.         } 
  315.     } 
  316.      
  317.     /** 
  318.      * debug 
  319.      *  
  320.      * @param mixed $debuginfo 
  321.      */ 
  322.     private function debug($debuginfo
  323.     { 
  324.         var_dump($debuginfo); 
  325.         exit(); 
  326.     } 
  327.      
  328.     /** 
  329.      * 輸出錯誤信息 
  330.      *  
  331.      * @param String $strErrMsg 
  332.      */ 
  333.     private function outputError($strErrMsg
  334.     { 
  335.         throw new Exception('MySQL Error: '.$strErrMsg); 
  336.     }  //開源軟件:Vevb.com 
  337.      
  338.     /** 
  339.      * destruct 關閉數據庫連接 
  340.      */ 
  341.     public function destruct() 
  342.     { 
  343.         $this->dbh = null; 
  344.     } 
  345. ?> 

調用方法:

  1. <?php 
  2. require 'MyPDO.class.php'
  3. $db = MyPDO::getInstance('localhost''root''123456''test''utf8'); 
  4.  
  5. //do something... 
  6.  
  7. $db->destruct(); 
  8. ?>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 榆社县| 海口市| 咸丰县| 长春市| 吐鲁番市| 马鞍山市| 海林市| 濮阳市| 宁阳县| 西贡区| 临汾市| 唐河县| 尼玛县| 沧州市| 平山县| 怀来县| 广德县| 湖州市| 平罗县| 黑河市| 开原市| 牙克石市| 南丰县| 五河县| 东海县| 闵行区| 阿鲁科尔沁旗| 安国市| 嘉黎县| 神农架林区| 蕲春县| 和平区| 广河县| 福安市| 南昌县| 道真| 马龙县| 疏勒县| 潜江市| 堆龙德庆县| 龙江县|