PDO是mysql數(shù)據(jù)庫操作的一個(gè)公用類了,我們不需要進(jìn)行自定類就可以直接使用pdo來操作數(shù)據(jù)庫了,但是在php默認(rèn)配置中pdo是未開啟所以我們必須先在php.ini中開啟它才可以使用。
需要開啟php的pdo支持,php5.1以上版本支持
實(shí)現(xiàn)數(shù)據(jù)庫連接單例化,有三要素 靜態(tài)變量、靜態(tài)實(shí)例化方法、私有構(gòu)造函數(shù) 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; //私有構(gòu)造函數(shù) 防止被直接實(shí)例化 private function __construct($dsn, $DBUser, $DBPwd, $longLink = false) { $this->DSN = $dsn; $this->DBUser = $DBUser; $this->DBPwd = $DBPwd; $this->longLink = $longLink; $this->connect(); } //私有 空克隆函數(shù) 防止被克隆 private function __clone(){} //靜態(tài) 實(shí)例化函數(shù) 返回一個(gè)pdo對(duì)象 static public function instance($dsn, $DBUser, $DBPwd, $longLink = false){ static $singleton = array();//靜態(tài)函數(shù) 用于存儲(chǔ)實(shí)例化對(duì)象 $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 //字段關(guān)聯(lián)數(shù)組處理, 主要用于寫入和更新數(shù)據(jù)、同and 或 or 的查詢條件,產(chǎn)生sql語句和映射字段的數(shù)組 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); } //用于處理單個(gè)字段處理 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); } //使用剛方法可以便捷產(chǎn)生查詢條件及對(duì)應(yīng)數(shù)據(jù)數(shù)組 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); }新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注