首页 > 开发 > PHP > 正文

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

2024-05-04 22:46:37
字体:
来源:转载
供稿:网友

本文实例讲述了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->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;  }}            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表