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程序设计有所帮助。
0
投稿
猜你喜欢
- 所以对应的asp处理代码如下代码如下:dedearr=split(xiangguanid2,chr(13)) '分割成数组
- bootstrap自带的响应式导航栏是向下滑动的,有时满足不了个性化的需求,需要做一个类似于android drawerLayout 侧滑的
- 本教程配置好后一劳永逸,不用再配置,每次只需要选择 Python 解释器即可打开KBEngine的服务器项目文件夹(资产目录)其主要工程目录
- 为了更好的进行封装,每个实现不同功能的js代码应该有自己的js文件,这样如果一个网页中引用了多个js文件,如下 <script typ
- 1. 错误提示 Warning: Cannot send session cookie - headers already sent War
- 在良好的数据库设计基础上,能有效地使用索引是SQL Server取得高性能的基础,SQL Server采用基于代价的优化模型,它对每一个提交
- 我们知道在PHP4里面,当我们使用一个类的时候,必须将该类所以文件使用include或require加进来,这样我们就可以使用类了。而对于P
- ajax.html <html><head> <met
- 如果用户输入的是直接插入到一个SQL语句中的查询,应用程序会很容易受到SQL注入,例如下面的例子:$unsafe_variable = $_
- 有时候我们会在页面上显示用户的所在地区,这个原理是:先得到用户的IP,然后去查询将IP转换成一个数值,最后去查这个数值所在的范围,来得到用户
- 如果不用类库(如jquery)来写,往往很多时候,都需要通过id或tag来获取html里的某一对象,然后对其进行操作。为了节省代码,把常用的
- 由于DOM(文档对象模型)概念的推出,这个API使HTML如虎添翼,但是有些学DHTML的朋友还是有些困挠,只是因为目前的手册的书写不太科学
- MySQL使用于认证目的的用户名,与Unix用户名(登录名字)或Windows用户名无关。缺省地,大多数MySQL客户尝试使用当前Unix用
- 简单的显示记录已经掌握,现在需要的就是通过ASP将信息内容插入到数据库中。一、拥有数据库cnbruce.mdb本数据库的作用就是用来 * 入数
- 概述基于Swoole的websocket服务,再之前的消息系统系列的第4篇,实现了更加复杂的业务场景,是对消息推送的完善和优化,代码本身就是
- 因为正则不够完善,所以代码中不能直接出现 <? 和 ?>如果是字符串,可以拆开写 "<" + &quo
- 现在电子商务网站的设计,正面临着一系列的挑战,其中最主要的挑战是:我们尝试建立一种用户体验,来提高用户在线购物的可能性。为了对抗网上激烈的竞
- 昨时要导一些数据,从网上搜到的。字段多时insert 语句生成的不完整了,还没有找到原因..有个缺点……就是标识种子的列 也insert了c
- smtp指令书接上文邮件实现详解,这里我们及我们简单复习一下smtp的指令如下:telnet smtp.163.com 25[outpout
- 一:命名空间里的namespace关键字和__NAMESPACE__常量的运用PHP支持两种抽象的访问当前命名空间内部元素的方法,__NAM