国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

php實現的pdo公共類定義與用法示例

2024-05-04 23:58:48
字體:
來源:轉載
供稿:網友

本文實例講述了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;  }}

實例:

<?phprequire_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教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 乌鲁木齐县| 黑山县| 美姑县| 大英县| 台湾省| 汝城县| 六枝特区| 时尚| 海宁市| 上栗县| 裕民县| 江门市| 旺苍县| 且末县| 潞西市| 永城市| 确山县| 新郑市| 乡宁县| 濮阳县| 东丰县| 进贤县| 彩票| 米脂县| 双峰县| 秀山| 察雅县| 沾化县| 阿尔山市| 石台县| 湘西| 商城县| 安平县| 蒙城县| 垫江县| 荃湾区| 高碑店市| 定安县| 商洛市| 南汇区| 屏山县|