<?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类的更多相关文章

  1. 一个PDO类

    下面是在网上借鉴的一个PDO类: <?php class Database{ private $host = DB_HOST; private $user = DB_USER; private ...

  2. PHP基于单例模式编写PDO类的方法

    一.单例模式简介 简单的说,一个对象(在学习设计模式之前,需要比较了解面向对象思想)只负责一个特定的任务: 二.为什么要使用PHP单例模式? 1.php的应用主要在于数据库应用, 所以一个应用中会存在 ...

  3. php-验证码类-PDO类-缩略图类

    Verify.class.php 验证码类 <?php class Verify{ const VERIFY_TYPE_NUM=1; const VERIFY_TYPE_EN=2; const ...

  4. 封装好的PDO类

    封装PDO类,方便使用: <?php header('content-type:text/html;charset=utf-8'); /** * 封装PDODB类 */ // 加载接口 // i ...

  5. PHP PDO类 单例

    <?php /*//pdo连接信息 $pdo=array("mysql:host=localhost;dbname=demo;charset=utf8","root ...

  6. pdo类的使用

    使用方法 2.php <?php require_once "./mypdo.php"; $pdo = DAOPDO::getInstance('localhost', 'r ...

  7. 学习到目前,自己封装的db类和pdo类

    DB封装类 <?php class DBDA { public $host = "localhost"; public $uid = "root"; pu ...

  8. PDO和PDOStatement类常用方法

    PDO — PDO 类 PDO::beginTransaction — 启动一个事务 PDO::commit — 提交一个事务 PDO::__construct — 创建一个表示数据库连接的 PDO ...

  9. PHP PDO 使用类

    PDO类 <?php class MYPDO { protected static $_instance = null; protected $dbName = ''; protected $d ...

随机推荐

  1. 淘淘相关DTO

    result 用于Controller层返回值或Controller于service层之间返回值 package com.taotao.common.pojo; import java.util.Li ...

  2. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) A B C D 水 模拟 构造

    A. Neverending competitions time limit per test 2 seconds memory limit per test 512 megabytes input ...

  3. Cropper

    jQuery.cropper是一款使用简单且功能强大的图片剪裁jQuery插件.该图片剪裁插件支持图片放大缩小,支持图片旋转,支持触摸屏设备,支持canvas,并且支持跨浏览器使用. 官网:https ...

  4. easing.js让页面动画丰富起来

    jQuery Easing是一款比较老的jQuery插件,在很多网站都有应用,尤其是在一些页面滚动.幻灯片切换等场景应用比较多.它非常小巧,且有多种动画方案供选择,使用简单,而且免费. <scr ...

  5. [LeetCode] 12. Integer to Roman ☆☆

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  6. 2015/8/30 Python基础(4):序列操作符

    序列是指成员有序排列,可以通过下标偏移量访问的类型.Python序列包括:字符串.列表和元组.序列的每个元素可以指定一个偏移量得到,多个元素是通过切片操作得到的.下标偏移量从0开始计数到总数-1结束. ...

  7. linux 下用 c 实现 ls -l 命令

    #include <stdio.h> #include <sys/types.h> #include <dirent.h> #include <sys/sta ...

  8. 微信公众号支付开发全过程(Java 版)

    一.微信官方文档微信支付开发流程(公众号支付) 首先我们到微信支付的官方文档的开发步骤部分查看一下需要的设置. [图片上传失败...(image-5eb825-1531014079742)] 因为微信 ...

  9. js_判断当前url是否合法http(s)

    alert(checkURL('http:555')); //false function checkURL(URL) { var str = URL, Expression = /http(s)?: ...

  10. hdu 1081 To The Max(dp+化二维为一维)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1081 To The Max Time Limit: 2000/1000 MS (Java/Others ...