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

php實現(xiàn)的pdo公共類定義與用法示例

 更新時間:2017年07月19日 12:08:38   作者:3wlog  
這篇文章主要介紹了php實現(xiàn)的pdo公共類定義與用法,結(jié)合具體實例形式分析了php實現(xiàn)的pdo操作類定義及查詢、插入等使用技巧,需要的朋友可以參考下

本文實例講述了php實現(xiàn)的pdo公共類定義與用法。分享給大家供大家參考,具體如下:

db.class.php :

<?php
class db extends \PDO {
  private static $_instance = null;
  protected $dbName = '';
  protected $dsn;
  protected $dbh;
  public function __construct($dbHost, $dbUser, $dbPasswd, $dbName, $dbCharset='utf8') {
    try {
      $this->dsn = 'mysql:host=' . $dbHost . ';dbname=' . $dbName;
      $this->dbh = new \PDO($this->dsn, $dbUser, $dbPasswd);
      $this->dbh->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
      $this->dbh->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
      $this->dbh->exec('SET character_set_connection='.$dbCharset.';SET character_set_client='.$dbCharset.';SET character_set_results='.$dbCharset);
    } catch (Exception $e) {
      $this->outputError($e->getMessage()); 
    }
  }
  public static function getInstance($dbHost, $dbUser, $dbPasswd, $dbName, $dbCharset='utf8') {
    if (self::$_instance === null) {
      self::$_instance = new self($dbHost, $dbUser, $dbPasswd, $dbName, $dbCharset);
    }
    return self::$_instance;
  }
  public function fetchAll($sql, $params = array()) {
    try {
      $stm = $this->dbh->prepare($sql);
      if ($stm && $stm->execute($params)) {
        return $stm->fetchAll(\PDO::FETCH_ASSOC);
      }
    } catch (Exception $e) {
      $this->outputError($e->getMessage());
    }
  }
  public function fetchOne($sql, $params = array()) {
    try {
      $result = false;
      $stm = $this->dbh->prepare($sql);
      if ($stm && $stm->execute($params)) {
        $result = $stm->fetch(\PDO::FETCH_ASSOC);
      }
      return $result;
    } catch (Exception $e) {
      $this->outputError($e->getMessage());
    }
  }
  public function fetchColumn($sql, $params = array()) {
    $result = '';
    try {
      $stm = $this->dbh->prepare($sql);
      if ($stm && $stm->execute($params)) {
        $result = $stm->fetchColumn();
      }
      return $result;
    } catch (Exception $e) {
      $this->outputError($e->getMessage());
    }
  }
  public function insert($table, $params = array(), $returnLastId = true) {
    $_implode_field = '';
    $fields = array_keys($params);
    $_implode_field = implode(',', $fields);
    $_implode_value = '';
    foreach ($fields as $value) {
      $_implode_value .= ':'. $value.',';
    }
    $_implode_value = trim($_implode_value, ',');
    $sql = 'INSERT INTO ' . $table . '(' . $_implode_field . ') VALUES ('.$_implode_value.')';
    try {
      $stm = $this->dbh->prepare($sql);
      $result = $stm->execute($params);
      if ( $returnLastId ) {
        $result = $this->dbh->lastInsertId();
      }
      return $result;
    } catch (Exception $e) {
      $this->outputError($e->getMessage());
    }
  }
  public function update($table, $params = array(), $where = null) {
    $_implode_field = '';
    $_implode_field_arr = array();
    if ( empty($where) ) {
      return false;
    }
    $fields = array_keys($params);
    foreach ($fields as $key) {
      $_implode_field_arr[] = $key . '=' . ':'.$key;
    }
    $_implode_field = implode(',', $_implode_field_arr);
    $sql = 'UPDATE ' . $table . ' SET ' . $_implode_field . ' WHERE ' . $where;
    try {
      $stm = $this->dbh->prepare($sql);
      $result = $stm->execute($params);
      return $result;
    } catch (Exception $e) {
      $this->outputError($e->getMessage());
    }
  }
  public function delete($sql, $params = array()) {
    try {
      $stm = $this->dbh->prepare($sql);
      $result = $stm->execute($params);
      return $result;
    } catch (Exception $e) {
      $this->outputError($e->getMessage());
    }
  }
  public function exec($sql, $params = array()) {
    try {
      $stm = $this->dbh->prepare($sql);
      $result = $stm->execute($params);
      return $result;
    } catch (Exception $e) {
      $this->outputError($e->getMessage());
    }
  }
  private function outputError($strErrMsg) {
    throw new Exception("MySQL Error: " . $strErrMsg);
  }
  public function __destruct() {
    $this->dbh = null;
  }
}

實例:

<?php
require_once './db.class.php';
$pdo = db::getInstance('127.0.0.1', 'root', '111111', 'php_cms');
$sql = "select id, title1 from cms_wz where id = :id limit 1";
$parame = array('id' => 12,);
$res = $pdo->fetchOne($sql, $parame);
var_dump($res);
$sql = 'SELECT * FROM cms_link';
$result = $db->fetchAll($sql);
print_r($result);
//查詢記錄數(shù)量
$sql = 'SELECT COUNT(*) FROM cms_link';
$count = $db->fetchColumn($sql);
echo $count;
$data = array(
  'siteid' => 1,
  'linktype' => 1,
  'name' => 'google',
  'url' => 'http://www.google.com',
  'listorder' => 0,
  'elite' => 0,
  'passed' => 1,
  'addtime' => time()
  );
$lastInsertId = $db->insert('cms_link', $data);
echo $lastInsertId;
//用 try
 try {
     $result = $pdo->insert('news', $essay);
   } catch (Exception $e) {
     error_log($e->getMessage());
     error_log($e->getMessage() . ' in ' . __FILE__ . ' on line ' . __LINE__);
     saveLog('url文章 : ' . $essay['link'] . '  數(shù)據(jù)插入失敗<br>');
     continue;
   }
$data = array(
  'siteid' => 1,
  'linktype' => 1,
  'name' => 'google',
  'url' => 'http://www.google.com',
  'listorder' => 0,
  'elite' => 0,
  'passed' => 1,
  'addtime' => time()
  );
$db->insert('cms_link', $data);
$sql = 'DELETE FROM cms_link WHERE linkid=4';
$result = $db->delete($sql);
var_dump($result);

更多關(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與以太坊交互詳解

    在這篇文章中我們給大家分享了關(guān)于PHP與以太坊交互的相關(guān)知識點(diǎn)內(nèi)容,有興趣的朋友們閱讀下吧。
    2018-08-08
  • php 執(zhí)行系統(tǒng)命令的方法

    php 執(zhí)行系統(tǒng)命令的方法

    在一個項目里用到這樣的東西,用另外一個服務(wù)做了一些事情,生成的文件權(quán)限,通過php 無法讀取,測試了很多種方式都沒能實現(xiàn),在網(wǎng)上找了下,可以用c寫一個代理來實現(xiàn),本人就實現(xiàn)了一下,果真可以。
    2009-07-07
  • WordPress主題制作之模板文件的引入方法

    WordPress主題制作之模板文件的引入方法

    這篇文章主要介紹了WordPress主題制作之模板文件的引入方法,針對get_template_part()函數(shù)的使用進(jìn)行了講解,需要的朋友可以參考下
    2015-12-12
  • php樹型類實例

    php樹型類實例

    這篇文章主要介紹了php樹型類,涉及數(shù)據(jù)結(jié)構(gòu)與算法中的樹結(jié)構(gòu),實例相對簡單易懂,對于學(xué)習(xí)數(shù)據(jù)結(jié)構(gòu)有一定的參考借鑒價值,需要的朋友可以參考下
    2014-12-12
  • PHP利用Socket獲取網(wǎng)站的SSL證書與公鑰

    PHP利用Socket獲取網(wǎng)站的SSL證書與公鑰

    這篇文章主要給大家介紹了PHP利用Socket獲取網(wǎng)站的SSL證書與公鑰的相關(guān)資料,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。
    2017-06-06
  • php定義一個參數(shù)帶有默認(rèn)值的函數(shù)實例分析

    php定義一個參數(shù)帶有默認(rèn)值的函數(shù)實例分析

    這篇文章主要介紹了php定義一個參數(shù)帶有默認(rèn)值的函數(shù),實例分析了php參數(shù)帶有默認(rèn)值的函數(shù)使用技巧,需要的朋友可以參考下
    2015-03-03
  • php 批量生成html,txt文件的實現(xiàn)代碼

    php 批量生成html,txt文件的實現(xiàn)代碼

    本篇文章是對使用php批量生成html,txt文件的實現(xiàn)代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • linux系統(tǒng)上支持php的 iconv()函數(shù)的方法

    linux系統(tǒng)上支持php的 iconv()函數(shù)的方法

    iconv函數(shù)庫能夠完成各種字符集間的轉(zhuǎn)換,是php編程中不可缺少的基礎(chǔ)函數(shù)庫。
    2011-10-10
  • 使用session判斷用戶登錄用戶權(quán)限(超簡單)

    使用session判斷用戶登錄用戶權(quán)限(超簡單)

    本篇文章是對session判斷用戶登錄用戶權(quán)限進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • php多層數(shù)組與對象的轉(zhuǎn)換實例代碼

    php多層數(shù)組與對象的轉(zhuǎn)換實例代碼

    通過json_decode(json_encode($object)可以將對象一次性轉(zhuǎn)換為數(shù)組,但是object中遇到非utf-8編碼的非ascii字符則會出現(xiàn)問題,比如gbk的中文,何況json_encode和decode的性能也值得疑慮
    2013-08-08

最新評論

丽水市| 灌云县| 家居| 方正县| 麻栗坡县| 双辽市| 左权县| 丽江市| 东安县| 平利县| 昌都县| 昭通市| 南通市| 赤城县| 武城县| 休宁县| 夹江县| 西乌珠穆沁旗| 霍林郭勒市| 汾西县| 东至县| 柘城县| 诸城市| 酒泉市| 青冈县| 泰兴市| 兴安县| 达拉特旗| 九寨沟县| 汽车| 交口县| 六盘水市| 汾阳市| 西和县| 花莲县| 大连市| 南充市| 土默特左旗| 奈曼旗| 万源市| 湾仔区|