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

首頁 > 網(wǎng)站 > 建站經(jīng)驗(yàn) > 正文

php_中PDO方式實(shí)現(xiàn)數(shù)據(jù)庫的增刪改查

2019-11-02 15:14:41
字體:
供稿:網(wǎng)友

   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); }
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 华宁县| 台州市| 琼中| 安阳市| 乃东县| 金塔县| 夏津县| 措美县| 远安县| 云龙县| 额敏县| 拜泉县| 武清区| 阜宁县| 乌海市| 定兴县| 日喀则市| 陵水| 大宁县| 江安县| 兴化市| 元谋县| 黎城县| 绥棱县| 肥城市| 札达县| 石台县| 灵丘县| 和政县| 宁南县| 东至县| 稻城县| 长葛市| 咸宁市| 汝阳县| 旅游| 监利县| 读书| 岫岩| 兰坪| 肇东市|