php基于PDO实现功能强大的MYSQL封装类实例
作者:小炒花生米 发布时间:2023-11-16 22:50:27
标签:php,PDO,MYSQL,封装类
本文实例讲述了php基于PDO实现功能强大的MYSQL封装类。分享给大家供大家参考,具体如下:
class CPdo{
protected $_dsn = "mysql:host=localhost;dbname=test";
protected $_name = "root";
protected $_pass = "";
protected $_condition = array();
protected $pdo;
protected $fetchAll;
protected $query;
protected $result;
protected $num;
protected $mode;
protected $prepare;
protected $row;
protected $fetchAction;
protected $beginTransaction;
protected $rollback;
protected $commit;
protected $char;
private static $get_mode;
private static $get_fetch_action;
/**
*pdo construct
*/
public function __construct($pconnect = false) {
$this->_condition = array(PDO::ATTR_PERSISTENT => $pconnect);
$this->pdo_connect();
}
/**
*pdo connect
*/
private function pdo_connect() {
try{
$this->pdo = new PDO($this->_dsn,$this->_name,$this->_pass,$this->_condition);
}
catch(Exception $e) {
return $this->setExceptionError($e->getMessage(), $e->getline, $e->getFile);
}
}
/**
*self sql get value action
*/
public function getValueBySelfCreateSql($sql, $fetchAction = "assoc",$mode = null) {
$this->fetchAction = $this->fetchAction($fetchAction);
$this->result = $this->setAttribute($sql, $this->fetchAction, $mode);
$this->AllValue = $this->result->fetchAll();
return $this->AllValue;
}
/**
*select condition can query
*/
private function setAttribute($sql, $fetchAction, $mode) {
$this->mode = self::getMode($mode);
$this->fetchAction = self::fetchAction($fetchAction);
$this->pdo->setAttribute(PDO::ATTR_CASE, $this->mode);
$this->query = $this->base_query($sql);
$this->query->setFetchMode($this->fetchAction);
return $this->query;
}
/**
*get mode action
*/
private static function getMode($get_style){
switch($get_style) {
case null:
self::$get_mode = PDO::CASE_NATURAL;
break;
case true:
self::$get_mode = PDO::CASE_UPPER;
break;
case false;
self::$get_mode= PDO::CASE_LOWER;
break;
}
return self::$get_mode;
}
/**
*fetch value action
*/
private static function fetchAction($fetchAction) {
switch($fetchAction) {
case "assoc":
self::$get_fetch_action = PDO::FETCH_ASSOC; //asso array
break;
case "num":
self::$get_fetch_action = PDO::FETCH_NUM; //num array
break;
case "object":
self::$get_fetch_action = PDO::FETCH_OBJ; //object array
break;
case "both":
self::$get_fetch_action = PDO::FETCH_BOTH; //assoc array and num array
break;
default:
self::$get_fetch_action = PDO::FETCH_ASSOC;
break;
}
return self::$get_fetch_action;
}
/**
*get total num action
*/
public function rowCount($sql) {
$this->result = $this->base_query($sql);
$this->num = $this->result->rowCount();
return $this->num;
}
/*
*simple query and easy query action
*/
public function query($table, $column = "*",$condition = array(), $group = "",$order = "", $having = "", $startSet = "",$endSet = "",$fetchAction = "assoc",$params = null){
$sql = "select ".$column." from `".$table."` ";
if ($condition != null) {
foreach($condition as $key=>$value) {
$where .= "$key = '$value' and ";
}
$sql .= "where $where";
$sql .= "1 = 1 ";
}
if ($group != "") {
$sql .= "group by ".$group." ";
}
if ($order != "") {
$sql .= " order by ".$order." ";
}
if ($having != "") {
$sql .= "having '$having' ";
}
if ($startSet != "" && $endSet != "" && is_numeric($endSet) && is_numeric($startSet)) {
$sql .= "limit $startSet,$endSet";
}
$this->result = $this->getValueBySelfCreateSql($sql, $fetchAction, $params);
return $this->result;
}
/**
*execute delete update insert and so on action
*/
public function exec($sql) {
$this->result = $this->pdo->exec($sql);
$substr = substr($sql, 0 ,6);
if ($this->result) {
return $this->successful($substr);
} else {
return $this->fail($substr);
}
}
/**
*prepare action
*/
public function prepare($sql) {
$this->prepare = $this->pdo->prepare($sql);
$this->setChars();
$this->prepare->execute();
while($this->rowz = $this->prepare->fetch()) {
return $this->row;
}
}
/**
*USE transaction
*/
public function transaction($sql) {
$this->begin();
$this->result = $this->pdo->exec($sql);
if ($this->result) {
$this->commit();
} else {
$this->rollback();
}
}
/**
*start transaction
*/
private function begin() {
$this->beginTransaction = $this->pdo->beginTransaction();
return $this->beginTransaction;
}
/**
*commit transaction
*/
private function commit() {
$this->commit = $this->pdo->commit();
return $this->commit;
}
/**
*rollback transaction
*/
private function rollback() {
$this->rollback = $this->pdo->rollback();
return $this->rollback;
}
/**
*base query
*/
private function base_query($sql) {
$this->setChars();
$this->query = $this->pdo->query($sql);
return $this->query;
}
/**
*set chars
*/
private function setChars() {
$this->char = $this->pdo->query("SET NAMES 'UTF8'");
return $this->char;
}
/**
*process sucessful action
*/
private function successful($params){
return "The ".$params." action is successful";
}
/**
*process fail action
*/
private function fail($params){
return "The ".$params." action is fail";
}
/**
*process exception action
*/
private function setExceptionError($getMessage, $getLine ,$getFile) {
echo "Error message is ".$getMessage."<br /> The Error in ".$getLine." line <br /> This file dir on ".$getFile;
exit();
}
}
希望本文所述对大家PHP程序设计有所帮助。


猜你喜欢
- 使用Tensorflow进行深度学习训练的时候,需要对训练好的网络模型和各种参数进行保存,以便在此基础上继续训练或者使用。介绍这方面的博客有
- 核心提示:VB读取MP3文件帧的信息比特率,采样频率,播放时间Private Sub Command1_Click()On Error Go
- win2000注册表程序 regedt32.exe下面是解决IIS出现Active Server Pages错误&
- MongoDB安装模块pip install pymongo连接数据库import pymongoclient = pymongo.Mong
- 本文实例讲述了PHP日期函数date格式化UNIX时间的方法。分享给大家供大家参考。具体分析如下:日期函数可以根据指定的格式将一个unix时
- Python 队列Queue和PriorityQueuePython的Queue模块适用于多线程编程的FIFO实现。它可用于在生产
- 1、到python官网 https://www.python.org 查找最新的原码,我使用的,Python-3.6.32、mkdir /h
- 新安装的MySQL5.7,登录时提示密码错误,安装的时候并没有更改密码,后来通过免密码登录的方式更改密码,输入update mysql.us
- 遇到了这个问题,意思是你的 CPU 支持AVX AVX2 (可以加速CPU计算),但你安装的 TensorFlow 版本不支持解决:1. 如
- 起因写这篇博客的起因是今天在刷leetcode的每日一题,是一道字符串转换整数 (atoi)的题,感兴趣的话可以点击题目名称去看一下具体描述
- 网站上传图片后生成缩略图应该是非常常用的功能了,通常来讲为了网站显示美观,缩略图会是同样尺寸,比如最近笔者做的一个站点,缩略图规格要求都是1
- 我们可以利用err对象来判断。当程序没有出现错误就说明已经执行了sql操作: sql="insert into
- group_concat()函数的参数是可以直接使用order by排序的。666。。下面通过例子来说明,首先看下面的t1表。比如,我们要查
- 问题描述 为了程序的正常运行,进行异常处理是有必要的,甚至于有时候,我们会主动的抛出异常,然后让程序进行异常捕获,再进行进一步的处理。但是,
- 本文实例为大家分享了tensorflow如何批量读取图片的具体代码,供大家参考,具体内容如下代码:import tensorflow as
- 这样还能减少CPU缓存命中失效的问题(点击这个链接来查看CPU的缓存是如何工作的以及MESI协议)。下面让我们来揭穿三个有关NULL位图的普
- 1:文件内容格式为json的数据如何解析import json,os,syscurrent_dir=os.path.abspath(&quo
- 字符串去除数字间的逗号在西文数字的表示中,很多格式是类似这样:123,456,789。如果得到这样的一个字符串,直接用int转换成整型肯定报
- Python 操作 MySQL配置win_64Ubuntu14.04Python3.xpip安装pymysql模块直接使用pip安装 pip
- 博主在这个问题上卡了挺久的,贴出来解决方法帮助需要的朋友,直接上代码(测试环境:win10+Python2.7):# coding=utf-