php實現(xiàn)的pdo公共類定義與用法示例
本文實例講述了php實現(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;
}
}
實例:
<?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ù)庫技巧總結(jié)》、《php+Oracle數(shù)據(jù)庫程序設(shè)計技巧總結(jié)》、《PHP+MongoDB數(shù)據(jù)庫操作技巧大全》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
- PHP基于PDO實現(xiàn)的SQLite操作類【包含增刪改查及事務(wù)等操作】
- php PDO實現(xiàn)的事務(wù)回滾示例
- php使用PDO事務(wù)配合表格讀取大量數(shù)據(jù)插入操作實現(xiàn)方法
- php的PDO事務(wù)處理機制實例分析
- PHP中PDO的事務(wù)處理分析
- php下pdo的mysql事務(wù)處理用法實例
- php中在PDO中使用事務(wù)(Transaction)
- ThinkPHP實現(xiàn)事務(wù)回滾示例代碼
- PHP操作MySQL事務(wù)實例
- php+mysql事務(wù)rollback&commit示例
- php實現(xiàn)基于pdo的事務(wù)處理方法示例
相關(guān)文章
php utf-8轉(zhuǎn)unicode的函數(shù)
php下我們想把uft-8,轉(zhuǎn)成unicode可以用下面的函數(shù)來實現(xiàn)2008-06-06
刪除數(shù)組元素實用的PHP數(shù)組函數(shù)
php之從數(shù)組中刪除空白的元素(包括只有空白字符的元素)將一個二維數(shù)組轉(zhuǎn)換為 hashmap2008-08-08
php實現(xiàn)專業(yè)獲取網(wǎng)站SEO信息類實例
這篇文章主要介紹了php實現(xiàn)專業(yè)獲取網(wǎng)站SEO信息類,實例分析了seoreport類針對網(wǎng)站SEO信息檢查與獲取的技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04

