本文实例讲述了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有序表查找之插值查找算法,简单分析了插值查找算法的概念、原理并结合实例形式分析了php实
ThinkPHP整合datatables实现服务端分页的示例代码下面小编就为大家分享一篇ThinkPHP整合datatables实现服务端分页的示例代码,具有很好的参考价值,希望对大家有所帮
PHP实现APP微信支付的实例讲解下面小编就为大家分享一篇PHP实现APP微信支付的实例讲解,具有很好的参考价值,希望对大家有所帮助。一起跟随小
PHP实现的多维数组排序算法分析这篇文章主要介绍了PHP实现的多维数组排序算法,结合实例形式对比分析了php针对多维数组及带有键名的多维数组进行
php+ajax实现无刷新文件上传功能(ajaxuploadfile)这篇文章主要为大家详细介绍了php结合ajaxuploadfile实现无刷新文件上传功能,具有一定的参考价值,感兴趣的小伙伴们
PHP的RSA加密解密方法以及开发接口使用本篇文章给大家详细介绍了PHP开发接口使用RSA进行加密解密方法,对此有兴趣的朋友可以学习下。