• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

简述php中的pdo公共类定义方法与用法详解

php 搞代码 3年前 (2022-01-22) 13次浏览 已收录 0个评论

这篇文章主要介绍了php实现的pdo公共类定义与用法,结合具体实例形式分析了php实现的pdo操作类定义及查询、插入等使用技巧,需要的朋友可以参考下

本文实例讲述了php实现的pdo公共类定义与用法。分享给大家供大家参考,具体如下:

db.class.php :

<?phpclass 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->ou<i style="color:transparent">本¥文来源gaodai$ma#com搞$代*码*网(</i><strong>搞代gaodaima码</strong>tputError($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;  }}

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:简述php中的pdo公共类定义方法与用法详解
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址