欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php实现的pdo公共类定义与用法示例

程序员文章站 2024-03-11 17:41:19
本文实例讲述了php实现的pdo公共类定义与用法。分享给大家供大家参考,具体如下: db.class.php :

本文实例讲述了php实现的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);
//查询记录数量
$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'] . '  数据插入失败<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);

更多关于php相关内容感兴趣的读者可查看本站专题:《php基于pdo操作数据库技巧总结》、《php+oracle数据库程序设计技巧总结》、《php+mongodb数据库操作技巧大全》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家php程序设计有所帮助。