PDO是mysql數據庫操作的一個公用類了,我們不需要進行自定類就可以直接使用pdo來操作數據庫了,但是在php默認配置中pdo是未開啟所以我們必須先在php.ini中開啟它才可以使用。
需要開啟php的pdo支持,php5.1以上版本支持
實現數據庫連接單例化,有三要素 靜態變量、靜態實例化方法、私有構造函數 DPDO.php
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 class DPDO{ private $DSN; private $DBUser; private $DBPwd; private $longLink; private $pdo; //私有構造函數 防止被直接實例化 private function __construct($dsn, $DBUser, $DBPwd, $longLink = false) { $this->DSN = $dsn; $this->DBUser = $DBUser; $this->DBPwd = $DBPwd; $this->longLink = $longLink; $this->connect(); } //私有 空克隆函數 防止被克隆 private function __clone(){} //靜態 實例化函數 返回一個pdo對象 static public function instance($dsn, $DBUser, $DBPwd, $longLink = false){ static $singleton = array();//靜態函數 用於存儲實例化對象 $singIndex = md5($dsn . $DBUser . $DBPwd . $longLink); if (empty($singleton[$singIndex])) { $singleton[$singIndex] = new self($dsn, $DBUser, $DBPwd, $longLink = false); } return $singleton[$singIndex]->pdo; } private function connect(){ try{ if($this->longLink){ $this->pdo = new PDO($this->DSN, $this->DBUser, $this->DBPwd, array(PDO::ATTR_PERSISTENT => true)); }else{ $this->pdo = new PDO($this->DSN, $this->DBUser, $this->DBPwd); } $this->pdo->query('SET NAMES UTF-8'); } catch(PDOException $e) { die('Error:' . $e->getMessage() . '<br/>'); } } }用於處理字段映射,使用pdo的字段映射,可以有效避免sql注入
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 //字段關聯數組處理, 主要用於寫入和更新數據、同and 或 or 的查詢條件,產生sql語句和映射字段的數組 public function FDFields($data, $link = ',', $judge = array(), $aliasTable = ''){ $sql = ''; $mapData = array(); foreach($data as $key => $value) { $mapIndex = ':' . ($link != ',' ? 'c' : '') . $aliasTable . $key; $sql .= ' ' . ($aliasTable ? $aliasTable . '.' : '') . '`' . $key . '` ' . ($judge[$key] ? $judge[$key] : '=') . ' ' . $mapIndex . ' ' . $link; $mapData[$mapIndex] = $value; } $sql = trim($sql, $link); return array($sql, $mapData); } //用於處理單個字段處理 public function FDField($field, $value, $judge = '=', $preMap = 'cn', $aliasTable = '') { $mapIndex = ':' . $preMap . $aliasTable . $field; $sql = ' ' . ($aliasTable ? $aliasTable . '.' : '') . '`' . $field . '`' . $judge . $mapIndex; $mapData[$mapIndex] = $value; return array($sql, $mapData); } //使用剛方法可以便捷產生查詢條件及對應數據數組 public function FDCondition($condition, $mapData) { if(is_string($condition)) { $where = $condition; } else if (is_array($condition)) { if($condition['str']) { if (is_string($condition['str'])) { $where = $condition['str']; } else { return false; } } if(is_array($condition['data'])) { $link = $condition['link'] ? $condition['link'] : 'and'; list($conSql, $mapConData) = $this->FDFields($condition['data'], $link, $condition['judge']); if ($conSql) { $where .= ($where ? ' ' . $link : '') . $conSql; $mapData = array_merge($mapData, $mapConData); } } } return array($where, $mapData); }增刪改查的具體實現DB.php
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 public function fetch($sql, $searchData = array(), $dataMode = PDO::FETCH_ASSOC, $preType = array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY)) { if ($sql) { $sql .= ' limit 1'; $pdoStatement = $this->pdo->prepare($sql, $preType); $pdoStatement->execute($searchData); return $data = $pdoStatement->fetch($dataMode); } else { return false; } } public function fetchAll($sql, $searchData = array(), $limit = array(0, 10), $dataMode = PDO::FETCH_ASSOC, $preType = array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY)) { if ($sql) { $sql .= ' limit ' . (int) $limit[0] . ',' . (intval($limit[1]) > 0 ? intval($limit[1]) : 10); $pdoStatement = $this->pdo->prepare($sql, $preType); $pdoStatement->execute($searchData); return $data = $pdoStatement->fetchAll($dataMode); } else { return false; } } public function insert($tableName, $data, $returnInsertId = false, $replace = false) { if(!e