欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2015年05月17日 09:57:23   投稿:hebedich  
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

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注入

//字段關(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);
  }

增刪改查的具體實(shí)現(xiàn)DB.php

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(!empty($tableName) && count($data) > 0){
      $sql = $replace ? 'REPLACE INTO ' : 'INSERT INTO ';
      list($setSql, $mapData) = $this->FDFields($data);
      $sql .= $tableName . ' set ' . $setSql;
      $pdoStatement = $this->pdo->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
      $execRet = $pdoStatement->execute($mapData);
      return $execRet ? ($returnInsertId ? $this->pdo->lastInsertId() : $execRet) : false;
    } else {
      return false;
    }
  }
   
  public function update($tableName, $data, $condition, $mapData = array(), $returnRowCount = true) {
    if(!empty($tableName) && count($data) > 0) {
      $sql = 'UPDATE ' . $tableName . ' SET ';
      list($setSql, $mapSetData) = $this->FDFields($data);
      $sql .= $setSql;
      $mapData = array_merge($mapData, $mapSetData);
      list($where, $mapData) = $this->FDCondition($condition, $mapData);
      $sql .= $where ? ' WHERE ' . $where : '';
      $pdoStatement = $this->pdo->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
      $execRet = $pdoStatement->execute($mapData);
      return $execRet ? ($returnRowCount ? $pdoStatement->rowCount() : $execRet) : false;
    } else {
      return false;
    }
  }
   
  public function delete($tableName, $condition, $mapData = array()) {
    if(!empty($tableName) && $condition){
      $sql = 'DELETE FROM ' . $tableName;
      list($where, $mapData) = $this->FDCondition($condition, $mapData);
      $sql .= $where ? ' WHERE ' . $where : '';
      $pdoStatement = $this->pdo->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
      $execRet = $pdoStatement->execute($mapData);
      return $execRet;
    }
  }

測(cè)試文件test.php

header("Content-type: text/html; charset=utf-8");
define('APP_DIR', dirname(__FILE__));
 
if (function_exists('spl_autoload_register')) {
  spl_autoload_register('autoClass');
} else {
  function __auto_load($className){
    autoClass($className);
  }
}
 
function autoClass($className){
  try{
    require_once APP_DIR.'/class/'.$className.'.php';
  } catch (Exception $e) {
    die('Error:' . $e->getMessage() . '<br />');
  }
}
$DB = new DB();
//插入
$inData['a'] = rand(1, 100);
$inData['b'] = rand(1, 1000);
$inData['c'] = rand(1,200) . '.' . rand(1,100);
$ret = $DB->insert('a', $inData);
echo '插入' . ($ret ? '成功' : '失敗') . '<br/>';
//更新
$upConData['a'] = 100;
$upConJudge['a'] = '<';
$upConData['b'] = 30;
$upConJudge['b'] = '>';
list($upConStr, $mapUpConData) = $DB->FDField('b', 200, '<', 'gt');
$condition = array(
  'str' => $upConStr,
  'data' => $upConData,
  'judge' => $upConJudge,
  'link' => 'and'
);
$upData['a'] = rand(1, 10);
$upData['b'] = 1;
$upData['c'] = 1.00;
$changeRows = $DB->update('a', $upData, $condition, $mapUpConData);
echo '更新行數(shù):' . (int) $changeRows . '<br/>';
//刪除
$delVal = rand(1, 10);
list($delCon, $mapDelCon) = $DB->FDField('a', $delVal);
$delRet = $DB->delete('a', $delCon, $mapDelCon);
echo '刪除a=' . $delVal . ($delRet ? '成功' : '失敗') . '<br/>';
 
//查詢
$data['a'] = '10';
$judge['a'] = '>';
$data['b'] = '400';
$judge['b'] = '<';
list($conSql, $mapConData) = $DB->FDFields($data, 'and', $judge);
$mData = $DB->fetch('select * from a where ' . $conSql . ' order by `a` desc', $mapConData);
 
var_dump($mData);

以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。

相關(guān)文章

  • 深入淺析PHP7.0新特征(五大新特征)

    深入淺析PHP7.0新特征(五大新特征)

    PHP7將在2015年10月正式發(fā)布,PHP7 ,將會(huì)是PHP腳本語言的重大版本更新,同時(shí)將帶來大幅的性能改進(jìn)和新的特性,以及改進(jìn)一些過時(shí)功能。該發(fā)布版本將會(huì)專注在性能加強(qiáng),源自PHP版本樹中的phpng分支
    2015-10-10
  • php 數(shù)據(jù)結(jié)構(gòu)之鏈表隊(duì)列

    php 數(shù)據(jù)結(jié)構(gòu)之鏈表隊(duì)列

    這篇文章主要介紹了php 數(shù)據(jù)結(jié)構(gòu)之鏈表隊(duì)列的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下
    2017-10-10
  • Laravel框架實(shí)現(xiàn)超簡(jiǎn)單的分頁效果示例

    Laravel框架實(shí)現(xiàn)超簡(jiǎn)單的分頁效果示例

    這篇文章主要介紹了Laravel框架實(shí)現(xiàn)超簡(jiǎn)單的分頁效果,結(jié)合實(shí)例形式分析了Laravel框架實(shí)現(xiàn)分頁功能的相關(guān)控制器、視圖、模板調(diào)用等相關(guān)操作技巧,需要的朋友可以參考下
    2019-02-02
  • PHP仿博客園 個(gè)人博客(2) 數(shù)據(jù)庫增添改刪

    PHP仿博客園 個(gè)人博客(2) 數(shù)據(jù)庫增添改刪

    先謝謝大家的鼓勵(lì)與支持,這是第2篇了。也是這個(gè)博客系統(tǒng)最核心的東西。這個(gè)博客寫完后,我會(huì)把它放在我的博客網(wǎng)站。這里也有我的一個(gè)簡(jiǎn)歷
    2013-07-07
  • 通過5個(gè)php實(shí)例細(xì)致說明傳值與傳引用的區(qū)別

    通過5個(gè)php實(shí)例細(xì)致說明傳值與傳引用的區(qū)別

    今天有個(gè)同事問我傳值和傳引用有什么不同,這讓我想起了,剛學(xué)php的時(shí)候,那個(gè)時(shí)候做過很多項(xiàng)目,做東西多,就以為自己php掌握的差不多了,隨著時(shí)間的推移,越深入的學(xué)習(xí),越覺得自己知道的真的很少,很少
    2012-08-08
  • thinkPHP中create方法與令牌驗(yàn)證實(shí)例淺析

    thinkPHP中create方法與令牌驗(yàn)證實(shí)例淺析

    這篇文章主要介紹了thinkPHP中create方法與令牌驗(yàn)證,以一個(gè)簡(jiǎn)單實(shí)例形式分析了thinkPHP中create方法與令牌驗(yàn)證增加表單安全性的相關(guān)技巧,代碼備有詳盡注釋說明,需要的朋友可以參考下
    2015-12-12
  • php去除換行(回車換行)的三種方法

    php去除換行(回車換行)的三種方法

    這篇文章主要介紹了php去除換行(回車換行)的三種方法,需要的朋友可以參考下
    2014-03-03
  • 發(fā)款php蜘蛛統(tǒng)計(jì)插件只要有mysql就可用

    發(fā)款php蜘蛛統(tǒng)計(jì)插件只要有mysql就可用

    有時(shí)候我們?yōu)榱丝匆幌轮┲肱佬械那闆r,不得不對(duì)日志進(jìn)行大量的分析,由此想做一款插件可以記錄蜘蛛的情況。在第一次做的時(shí)候,只是記錄下蜘蛛的爬行次數(shù),不大好分析。
    2010-10-10
  • 詳解Yii2 rules 的驗(yàn)證規(guī)則

    詳解Yii2 rules 的驗(yàn)證規(guī)則

    這篇文章主要介紹了Yii2 rules 的驗(yàn)證規(guī)則,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-12-12
  • 如何動(dòng)態(tài)查看及加載PHP擴(kuò)展

    如何動(dòng)態(tài)查看及加載PHP擴(kuò)展

    這篇文章主要介紹了如何動(dòng)態(tài)查看及加載PHP擴(kuò)展,幫助大家更好的理解和學(xué)習(xí)使用PHP,感興趣的朋友可以了解下
    2021-04-04

最新評(píng)論