最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

php基于PDO實現(xiàn)功能強(qiáng)大的MYSQL封裝類實例

 更新時間:2017年02月27日 11:28:05   作者:小炒花生米  
這篇文章主要介紹了php基于PDO實現(xiàn)功能強(qiáng)大的MYSQL封裝類,結(jié)合完整實例形式分析了php基于pdo實現(xiàn)mysql數(shù)據(jù)庫連接、增刪改查、事務(wù)等操作的方法,需要的朋友可以參考下

本文實例講述了php基于PDO實現(xiàn)功能強(qiáng)大的MYSQL封裝類。分享給大家供大家參考,具體如下:

class CPdo{
 protected $_dsn = "mysql:host=localhost;dbname=test";
 protected $_name = "root";
 protected $_pass = "";
 protected $_condition = array();
 protected $pdo;
 protected $fetchAll;
 protected $query;
 protected $result;
 protected $num;
 protected $mode;
 protected $prepare;
 protected $row;
 protected $fetchAction;
 protected $beginTransaction;
 protected $rollback;
 protected $commit;
 protected $char;
 private static $get_mode;
 private static $get_fetch_action;
 /**
 *pdo construct
 */
 public function __construct($pconnect = false) {
  $this->_condition = array(PDO::ATTR_PERSISTENT => $pconnect);
  $this->pdo_connect();
 }
 /**
 *pdo connect
 */
 private function pdo_connect() {
  try{
   $this->pdo = new PDO($this->_dsn,$this->_name,$this->_pass,$this->_condition);
  }
  catch(Exception $e) {
   return $this->setExceptionError($e->getMessage(), $e->getline, $e->getFile);
  }
 }
 /**
 *self sql get value action
 */
 public function getValueBySelfCreateSql($sql, $fetchAction = "assoc",$mode = null) {
  $this->fetchAction = $this->fetchAction($fetchAction);
  $this->result = $this->setAttribute($sql, $this->fetchAction, $mode);
  $this->AllValue = $this->result->fetchAll();
  return $this->AllValue;
 }
 /**
 *select condition can query
 */
 private function setAttribute($sql, $fetchAction, $mode) {
  $this->mode = self::getMode($mode);
  $this->fetchAction = self::fetchAction($fetchAction);
  $this->pdo->setAttribute(PDO::ATTR_CASE, $this->mode);
  $this->query = $this->base_query($sql);
  $this->query->setFetchMode($this->fetchAction);
  return $this->query;
 }
 /**
 *get mode action
 */
 private static function getMode($get_style){
  switch($get_style) {
   case null:
    self::$get_mode = PDO::CASE_NATURAL;
   break;
   case true:
    self::$get_mode = PDO::CASE_UPPER;
   break;
   case false;
   self::$get_mode= PDO::CASE_LOWER;
   break;
  }
  return self::$get_mode;
 }
 /**
 *fetch value action
 */
 private static function fetchAction($fetchAction) {
  switch($fetchAction) {
   case "assoc":
    self::$get_fetch_action = PDO::FETCH_ASSOC; //asso array
   break;
   case "num":
    self::$get_fetch_action = PDO::FETCH_NUM; //num array
   break;
   case "object":
    self::$get_fetch_action = PDO::FETCH_OBJ; //object array
   break;
   case "both":
    self::$get_fetch_action = PDO::FETCH_BOTH; //assoc array and num array
   break;
   default:
    self::$get_fetch_action = PDO::FETCH_ASSOC;
   break;
  }
  return self::$get_fetch_action;
 }
 /**
 *get total num action
 */
 public function rowCount($sql) {
  $this->result = $this->base_query($sql);
  $this->num = $this->result->rowCount();
  return $this->num;
 }
 /*
 *simple query and easy query action
 */
 public function query($table, $column = "*",$condition = array(), $group = "",$order = "", $having = "", $startSet = "",$endSet = "",$fetchAction = "assoc",$params = null){
  $sql = "select ".$column." from `".$table."` ";
  if ($condition != null) {
   foreach($condition as $key=>$value) {
    $where .= "$key = '$value' and ";
   }
   $sql .= "where $where";
   $sql .= "1 = 1 ";
  }
  if ($group != "") {
   $sql .= "group by ".$group." ";
  }
  if ($order != "") {
   $sql .= " order by ".$order." ";
  }
  if ($having != "") {
   $sql .= "having '$having' ";
  }
  if ($startSet != "" && $endSet != "" && is_numeric($endSet) && is_numeric($startSet)) {
   $sql .= "limit $startSet,$endSet";
  }
  $this->result = $this->getValueBySelfCreateSql($sql, $fetchAction, $params);
  return $this->result;
 }
 /**
 *execute delete update insert and so on action
 */
 public function exec($sql) {
  $this->result = $this->pdo->exec($sql);
  $substr = substr($sql, 0 ,6);
  if ($this->result) {
   return $this->successful($substr);
  } else {
   return $this->fail($substr);
  }
 }
 /**
 *prepare action
 */
 public function prepare($sql) {
  $this->prepare = $this->pdo->prepare($sql);
  $this->setChars();
  $this->prepare->execute();
  while($this->rowz = $this->prepare->fetch()) {
   return $this->row;
  }
 }
 /**
 *USE transaction
 */
 public function transaction($sql) {
  $this->begin();
  $this->result = $this->pdo->exec($sql);
  if ($this->result) {
   $this->commit();
  } else {
   $this->rollback();
  }
 }
 /**
 *start transaction
 */
 private function begin() {
  $this->beginTransaction = $this->pdo->beginTransaction();
  return $this->beginTransaction;
 }
 /**
 *commit transaction
 */
 private function commit() {
  $this->commit = $this->pdo->commit();
  return $this->commit;
 }
 /**
 *rollback transaction
 */
 private function rollback() {
  $this->rollback = $this->pdo->rollback();
  return $this->rollback;
 }
 /**
 *base query
 */
 private function base_query($sql) {
  $this->setChars();
  $this->query = $this->pdo->query($sql);
  return $this->query;
 }
 /**
 *set chars
 */
 private function setChars() {
  $this->char = $this->pdo->query("SET NAMES 'UTF8'");
  return $this->char;
 }
 /**
 *process sucessful action 
 */
 private function successful($params){
  return "The ".$params." action is successful";
 }
 /**
 *process fail action
 */
 private function fail($params){
  return "The ".$params." action is fail";
 }
 /**
 *process exception action
 */
 private function setExceptionError($getMessage, $getLine ,$getFile) {
  echo "Error message is ".$getMessage."<br /> The Error in ".$getLine." line <br /> This file dir on ".$getFile;
  exit();
 }
}

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP基于pdo操作數(shù)據(jù)庫技巧總結(jié)》、《php+Oracle數(shù)據(jù)庫程序設(shè)計技巧總結(jié)》、《PHP+MongoDB數(shù)據(jù)庫操作技巧大全》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家PHP程序設(shè)計有所幫助。

相關(guān)文章

  • PHP 采集心得技巧

    PHP 采集心得技巧

    今天給大家共享一下我的采集代碼! 思路: 采集程序的思路很簡單大體可以分為以下幾個步驟
    2009-05-05
  • PHP+Apache環(huán)境中如何隱藏Apache版本

    PHP+Apache環(huán)境中如何隱藏Apache版本

    以PHP+Apache服務(wù)器環(huán)境為例,給大家講解如何能夠隱藏Apache的版本號以及具體做法。
    2017-11-11
  • php+js實現(xiàn)百度地圖多點標(biāo)注的方法

    php+js實現(xiàn)百度地圖多點標(biāo)注的方法

    這篇文章主要介紹了php+js實現(xiàn)百度地圖多點標(biāo)注的方法,涉及php結(jié)合js針對百度地圖接口調(diào)用與json操作相關(guān)技巧,需要的朋友可以參考下
    2016-11-11
  • 學(xué)習(xí)php設(shè)計模式 php實現(xiàn)備忘錄模式(Memento)

    學(xué)習(xí)php設(shè)計模式 php實現(xiàn)備忘錄模式(Memento)

    這篇文章主要介紹了php設(shè)計模式中的備忘錄模式,使用php實現(xiàn)備忘錄模式,感興趣的小伙伴們可以參考一下
    2015-12-12
  • php通過修改header強(qiáng)制圖片下載的方法

    php通過修改header強(qiáng)制圖片下載的方法

    這篇文章主要介紹了php通過修改header強(qiáng)制圖片下載的方法,實例分析了php強(qiáng)制圖片下載的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-03-03
  • 在php7中MongoDB實現(xiàn)模糊查詢的方法詳解

    在php7中MongoDB實現(xiàn)模糊查詢的方法詳解

    MongoDB模糊查詢語句相信對大家來說都不陌生,這篇文章主要給大家介紹了在php 7中MongoDB實現(xiàn)模糊查詢的方法,文中給出了詳細(xì)的介紹和示例代碼,對大家具有一定的參考學(xué)習(xí)價值,需要的朋友一起來看看吧。
    2017-05-05
  • PHP7新功能總結(jié)

    PHP7新功能總結(jié)

    在本文里我們給大家介紹了關(guān)于PHP7新功能以及相關(guān)知識點內(nèi)容,正在學(xué)習(xí)PHP7的朋友們參考下。
    2019-04-04
  • php排序算法實例分析

    php排序算法實例分析

    這篇文章主要介紹了php排序算法,結(jié)合實例形式分析了php數(shù)據(jù)查詢、排序、數(shù)組去重、遍歷與排序的相關(guān)操作技巧與注意事項,需要的朋友可以參考下
    2016-10-10
  • PHP實現(xiàn)websocket通信的方法示例

    PHP實現(xiàn)websocket通信的方法示例

    這篇文章主要介紹了PHP實現(xiàn)websocket通信的方法,結(jié)合實例形式分析了php基于websocket類的socket通信相關(guān)客戶端與服務(wù)器端操作技巧,需要的朋友可以參考下
    2018-08-08
  • 解析如何用php screw加密php源代碼

    解析如何用php screw加密php源代碼

    本篇文章是對用php_screw加密php源代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06

最新評論

页游| 天长市| 丹棱县| 南昌县| 岫岩| 高雄市| 翼城县| 连江县| 许昌县| 沐川县| 德保县| 祁东县| 揭西县| 尤溪县| 泗洪县| 大安市| 永昌县| 台安县| 左云县| 三都| 泸溪县| 沭阳县| 稻城县| 郓城县| 安国市| 中卫市| 百色市| 鄂伦春自治旗| 哈巴河县| 赫章县| 广州市| 鸡泽县| 涟源市| 秦皇岛市| 肃宁县| 当雄县| 调兵山市| 四川省| 包头市| 潜江市| 馆陶县|