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 ...
随机推荐
- qt4+vs2010 环境搭建
1.安装开发所需的软件: vs2010(包括VS2010SP1dvd1,Visual_Assist_X_10.9.2062.0_Crack等) QT: qt-win-opensource-4.8.5- ...
- HDU1540 区间合并
Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- Linux下实现文档在线浏览
使用php实现百度文库功能,网上搜索到的方案,实现doc转pdf,pdf转swf,然后显示出来. 这里简单的记录下,[doc转pdf,pdf转swf]两个功能的搭建流程. doc转pdf 使用到下列程 ...
- C语言实现单链表的遍历,逆序,插入,删除
单链表的遍历,逆序,插入,删除 #include<stdio.h> #include<stdlib.h> #include <string.h> #define b ...
- Web Service快速入门
一言以蔽之:WebService是一种跨编程语言和跨操作系统平台的远程调用技术. 那么它是如何做到这种跨语言,跨平台之间的调用呢? 其实它是以一个xml文件以及webservice这种服务来实现跨平台 ...
- BZOJ2733:使用并查集维护连通性之后用线段树维护+线段树合并(动态开点)
可以说是线段树合并的裸题吧 题意就是给你两个操作 一个操作是合并两个集合,这两个集合都是用权值线段树维护的,便于查询第k小元素 另一个操作就是查询区间极值了 #include<cstdio> ...
- [洛谷P3460] [POI2007]TET-Tetris Attack
洛谷题目链接:[POI2007]TET-Tetris Attack 题目描述 A puzzle called "Tetris Attack" has lately become a ...
- bzoj4695 最假女选手
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4695 [题解] SegmentTree beats!(见jiry_2论文/营员交流) 考虑只 ...
- linux内核网络接收数据流程图【转】
转自:http://blog.chinaunix.net/uid-23069658-id-3141409.html 4.3 数据接收流程图 各层主要函数以及位置功能说明: 1)s ...
- 64_j1
JSCookMenu-2.0.4-13.fc26.noarch.rpm 13-Feb-2017 22:06 38098 Java-WebSocket-1.3.1-0.2.git58d1778.fc24 ...