PHP PDO类
<?php
//数据库连接类,不建议直接使用DB,而是对DB封装一层
//这个类不会被污染,不会被直接调用
class DB {
//pdo对象
private $_pdo = null;
//用于存放实例化的对象
static private $_instance = null; //公共静态方法获取实例化的对象
static protected function getInstance() {
if (!(self::$_instance instanceof self)) {
self::$_instance = new self();
}
return self::$_instance;
} //私有克隆
private function __clone() {} //私有构造
private function __construct() {
try {
$this->_pdo = new PDO(DB_DNS, DB_USER, DB_PASS, array(PDO::MYSQL_ATTR_INIT_COMMAND=>'SET NAMES '.DB_CHARSET));
$this->_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
exit($e->getMessage());
}
} //新增
protected function add($_tables, Array $_addData) {
$_addFields = array();
$_addValues = array();
foreach ($_addData as $_key=>$_value) {
$_addFields[] = $_key;
$_addValues[] = $_value;
}
$_addFields = implode(',', $_addFields);
$_addValues = implode("','", $_addValues);
$_sql = "INSERT INTO $_tables[0] ($_addFields) VALUES ('$_addValues')";
return $this->execute($_sql)->rowCount();
} //修改
protected function update($_tables, Array $_param, Array $_updateData) {
$_where = $_setData = '';
foreach ($_param as $_key=>$_value) {
$_where .= $_value.' AND ';
}
$_where = 'WHERE '.substr($_where, 0, -4);
foreach ($_updateData as $_key=>$_value) {
if (Validate::isArray($_value)) {
$_setData .= "$_key=$_value[0],";
} else {
$_setData .= "$_key='$_value',";
}
}
$_setData = substr($_setData, 0, -1);
$_sql = "UPDATE $_tables[0] SET $_setData $_where";
return $this->execute($_sql)->rowCount();
} //验证一条数据
protected function isOne($_tables, Array $_param) {
$_where = '';
foreach ($_param as $_key=>$_value) {
$_where .=$_value.' AND ';
}
$_where = 'WHERE '.substr($_where, 0, -4);
$_sql = "SELECT id FROM $_tables[0] $_where LIMIT 1";
return $this->execute($_sql)->rowCount();
} //删除
protected function delete($_tables, Array $_param) {
$_where = '';
foreach ($_param as $_key=>$_value) {
$_where .= $_value.' AND ';
}
$_where = 'WHERE '.substr($_where, 0, -4);
$_sql = "DELETE FROM $_tables[0] $_where LIMIT 1";
return $this->execute($_sql)->rowCount();
} //查询
protected function select($_tables, Array $_fileld, Array $_param = array()) {
$_limit = $_order = $_where = $_like = '';
if (Validate::isArray($_param) && !Validate::isNullArray($_param)) {
$_limit = isset($_param['limit']) ? 'LIMIT '.$_param['limit'] : '';
$_order = isset($_param['order']) ? 'ORDER BY '.$_param['order'] : '';
if (isset($_param['where'])) {
foreach ($_param['where'] as $_key=>$_value) {
$_where .= $_value.' AND ';
}
$_where = 'WHERE '.substr($_where, 0, -4);
}
if (isset($_param['like'])) {
foreach ($_param['like'] as $_key=>$_value) {
$_like = "WHERE $_key LIKE '%$_value%'";
}
}
}
$_selectFields = implode(',', $_fileld);
$_table = isset($_tables[1]) ? $_tables[0].','.$_tables[1] : $_tables[0];
$_sql = "SELECT $_selectFields FROM $_table $_where $_like $_order $_limit";
$_stmt = $this->execute($_sql);
$_result = array();
while (!!$_objs = $_stmt->fetchObject()) {
$_result[] = $_objs;
}
return Tool::setHtmlString($_result);
} //总记录
protected function total($_tables, Array $_param = array()) {
$_where = '';
if (isset($_param['where'])) {
foreach ($_param['where'] as $_key=>$_value) {
$_where .= $_value.' AND ';
}
$_where = 'WHERE '.substr($_where, 0, -4);
}
$_sql = "SELECT COUNT(*) as count FROM $_tables[0] $_where";
$_stmt = $this->execute($_sql);
return $_stmt->fetchObject()->count;
} //得到下一个ID
protected function nextId($_tables) {
$_sql = "SHOW TABLE STATUS LIKE '$_tables[0]'";
$_stmt = $this->execute($_sql);
return $_stmt->fetchObject()->Auto_increment;
} //执行SQL
private function execute($_sql) {
try {
$_stmt = $this->_pdo->prepare($_sql);
$_stmt->execute();
} catch (PDOException $e) {
exit('SQL语句:'.$_sql.'<br />错误信息:'.$e->getMessage());
}
return $_stmt;
}
}
?>
PHP PDO类的更多相关文章
- 一个PDO类
下面是在网上借鉴的一个PDO类: <?php class Database{ private $host = DB_HOST; private $user = DB_USER; private ...
- PHP基于单例模式编写PDO类的方法
一.单例模式简介 简单的说,一个对象(在学习设计模式之前,需要比较了解面向对象思想)只负责一个特定的任务: 二.为什么要使用PHP单例模式? 1.php的应用主要在于数据库应用, 所以一个应用中会存在 ...
- php-验证码类-PDO类-缩略图类
Verify.class.php 验证码类 <?php class Verify{ const VERIFY_TYPE_NUM=1; const VERIFY_TYPE_EN=2; const ...
- 封装好的PDO类
封装PDO类,方便使用: <?php header('content-type:text/html;charset=utf-8'); /** * 封装PDODB类 */ // 加载接口 // i ...
- PHP PDO类 单例
<?php /*//pdo连接信息 $pdo=array("mysql:host=localhost;dbname=demo;charset=utf8","root ...
- pdo类的使用
使用方法 2.php <?php require_once "./mypdo.php"; $pdo = DAOPDO::getInstance('localhost', 'r ...
- 学习到目前,自己封装的db类和pdo类
DB封装类 <?php class DBDA { public $host = "localhost"; public $uid = "root"; pu ...
- PDO和PDOStatement类常用方法
PDO — PDO 类 PDO::beginTransaction — 启动一个事务 PDO::commit — 提交一个事务 PDO::__construct — 创建一个表示数据库连接的 PDO ...
- PHP PDO 使用类
PDO类 <?php class MYPDO { protected static $_instance = null; protected $dbName = ''; protected $d ...
随机推荐
- 阐述ArrayList、Vector、LinkedList的存储性能和特性?
ArrayList 和Vector他们底层的实现都是一样的,都是使用数组方式存储数据,此数组元素数大于实际存储的数据以便增加和插入元素,它们都允许直接按序号索引元素,但是插入元素要涉及数组元素移动等内 ...
- Java的四种引用?用到的场景?
在JDK 1.2以前的版本中,若一个对象不被任何变量引用,那么程序就无法再使用这个对象.也就是说,只有对象处于可触及(reachable)状态,程序才能使用它.从JDK 1.2版本开始,把对象的引用分 ...
- Android 解决adb server is out of date. killing... ADB server didn't ACK * failed to star
The connection to adb is down, and a severe error has occured. [-- :: - HelloOPone] You must restart ...
- RotateAnimation 详解
RotateAnimation 详解 看看新闻网>看引擎>开源产品 其他构造器的旋转也可参考这副图. RotateAnimation旋转坐标系为以旋转点为坐标系(0,0)点.x轴为0度,顺 ...
- [LeetCode] Sort Colors 对于元素取值有限的数组,只遍历一遍的排序方法
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- .net 跨域 问题解决
参考地址:http://www.cnblogs.com/moretry/p/4154479.html 在项目上面使用 Nuget 搜索 microsoft.aspnet.webapi.cors 直接下 ...
- 基于Mysql数据库亿级数据下的分库分表方案
移动互联网时代,海量的用户数据每天都在产生,基于用户使用数据的用户行为分析等这样的分析,都需要依靠数据都统计和分析,当数据量小时,问题没有暴露出来,数据库方面的优化显得不太重要,一旦数据量越来越大时, ...
- LightOJ 1278 - Sum of Consecutive Integers 分解奇因子 + 思维
http://www.lightoj.com/volume_showproblem.php?problem=1278 题意:问一个数n能表示成几种连续整数相加的形式 如6=1+2+3,1种. 思路:先 ...
- WCF使用注意事项
执行如下 批处理:"C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\svcutil.exe" http://127.0.0.1: ...
- 【BZOJ1879】【SDOI2009】Bill的挑战 [状压DP]
Bill的挑战 Time Limit: 4 Sec Memory Limit: 64 MB[Submit][Status][Discuss] Description Input 第一行:一个整数T, ...