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

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

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

本文實(shí)例講述了php實(shí)現(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;
  }
}

實(shí)例:

<?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ù)庫(kù)技巧總結(jié)》、《php+Oracle數(shù)據(jù)庫(kù)程序設(shè)計(jì)技巧總結(jié)》、《PHP+MongoDB數(shù)據(jù)庫(kù)操作技巧大全》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見數(shù)據(jù)庫(kù)操作技巧匯總

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

相關(guān)文章

  • php utf-8轉(zhuǎn)unicode的函數(shù)

    php utf-8轉(zhuǎn)unicode的函數(shù)

    php下我們想把uft-8,轉(zhuǎn)成unicode可以用下面的函數(shù)來(lái)實(shí)現(xiàn)
    2008-06-06
  • 基于empty函數(shù)的輸出詳解

    基于empty函數(shù)的輸出詳解

    本篇文章是對(duì)empty函數(shù)的輸出進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • 刪除數(shù)組元素實(shí)用的PHP數(shù)組函數(shù)

    刪除數(shù)組元素實(shí)用的PHP數(shù)組函數(shù)

    php之從數(shù)組中刪除空白的元素(包括只有空白字符的元素)將一個(gè)二維數(shù)組轉(zhuǎn)換為 hashmap
    2008-08-08
  • 淺析is_writable的php實(shí)現(xiàn)

    淺析is_writable的php實(shí)現(xiàn)

    本篇文章是對(duì)is_writable的php實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • php實(shí)現(xiàn)專業(yè)獲取網(wǎng)站SEO信息類實(shí)例

    php實(shí)現(xiàn)專業(yè)獲取網(wǎng)站SEO信息類實(shí)例

    這篇文章主要介紹了php實(shí)現(xiàn)專業(yè)獲取網(wǎng)站SEO信息類,實(shí)例分析了seoreport類針對(duì)網(wǎng)站SEO信息檢查與獲取的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-04-04
  • php提高腳本性能的4個(gè)技巧

    php提高腳本性能的4個(gè)技巧

    這篇文章主要介紹了php提高腳本性能的4個(gè)技巧,有助于提高程序的運(yùn)行速度,感興趣的朋友可以了解下
    2020-08-08
  • PHP自毀程序(慎用)

    PHP自毀程序(慎用)

    這篇文章主要介紹了PHP自毀程序,可實(shí)現(xiàn)遞歸刪除當(dāng)前目錄及數(shù)據(jù)庫(kù)的功能,完成當(dāng)前程序的卸載,用戶需謹(jǐn)慎使用,需要的朋友可以參考下
    2015-07-07
  • php自定義時(shí)間轉(zhuǎn)換函數(shù)示例

    php自定義時(shí)間轉(zhuǎn)換函數(shù)示例

    這篇文章主要介紹了php自定義時(shí)間轉(zhuǎn)換函數(shù),可實(shí)現(xiàn)時(shí)間戳與字符串時(shí)間顯示格式之間的轉(zhuǎn)換功能,需要的朋友可以參考下
    2016-12-12
  • 詳解WordPress中給鏈接添加查詢字符串的方法

    詳解WordPress中給鏈接添加查詢字符串的方法

    這篇文章主要介紹了詳解WordPress中給鏈接添加查詢字符串的方法,依靠add_query_arg()函數(shù)來(lái)實(shí)現(xiàn),需要的朋友可以參考下
    2015-12-12
  • 一個(gè)PHP分頁(yè)類的代碼

    一個(gè)PHP分頁(yè)類的代碼

    最近有朋友問我能不能寫個(gè)php分頁(yè)類,我仔細(xì)考慮了一下,其實(shí),我已經(jīng)有一段時(shí)間沒碰過這個(gè)東西了,說(shuō)點(diǎn)經(jīng)驗(yàn),寫點(diǎn)簡(jiǎn)單的還行,要是寫個(gè)太復(fù)雜的,是挺有點(diǎn)費(fèi)勁的
    2011-05-05

最新評(píng)論