PDO简单的DB类封装
<?php
class DB
{
private $dbs = "";
private $fields = "*";
private $tables = null;
private $joins = null;
private $ons = null;
private $wheres = null;
private $limits = null;
private $orderby = null;
private $likes = null;
private $groupby = null;
private $havings = null;
private $sums = null;
private $field = null;
private $avgs = null;
private $counts = null;
private $maxs = null;
private $mins = null;
// 链接数据库
function __construct()
{
try {
$this->dbs = new PDO('mysql:host=主机地址;dbname=表名','用户名','密码');
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
}
// 各种查询
// 指定字段查询
public function field($data){
$this->fields = implode(",",$data);
return $this;
}
// 要查询的表名
public function table($table){
$this->tables = $table;
return $this;
}
// 内链接
public function join($table){
$this->joins = " inner join ".$table;
return $this;
}
// 左连接
public function leftjoin($table){
$this->joins = " left join ".$table;
return $this;
}
// 右链接
public function rightjoin($table){
$this->joins = " right join ".$table;
return $this;
}
// 关联关系
public function on($id,$gid){
$this->ons = " on ".$id.'='.$gid;
return $this;
}
// 分组后条件
public function having($k,$v){
$this->havings = " having ".$k.'='.$v;
return $this;
}
//条件
public function where($k,$v){
$this->wheres = " where ".$k.'='.$v;
return $this;
}
// 限制条件
public function limit($origin,$end){
$this->limits = " limit ".$origin.",".$end;
return $this;
}
public function order($id,$asc){
$this->orderby = " order by ".$id." ".$asc;
return $this;
}
// 模糊查询
public function like($k,$v){
$this->likes = " where"." ".$k." "." "." like "."'".'%'.$v.'%'."'";
return $this;
}
// 分组查询
public function group($name){
$this->groupby = " group by ".$name;
return $this;
}
// 求和
public function sum($v){
$this->sums = " sum"."(".$v.")"." ";
return $this;
}
// 平均值
public function avg($v){
$this->sums = " avg"."(".$v.")"." ";
return $this;
}
// 长度
public function count($v){
$this->counts = " count"."(".$v.")"." ";
return $this;
}
// 最大值
public function max($v){
$this->maxs = " max"."(".$v.")"." ";
return $this;
}
// 最小值
public function min($v){
$this->mins = " min"."(".$v.")"." ";
return $this;
}
// 查询语句
public function get(){
if ($this->sums == "" && $this->avgs == "" && $this->counts == "" && $this->maxs == "" && $this->mins == "") {
$sql = "select ".$this->fields." from ".$this->tables.$this->joins.$this->ons.$this->wheres.$this->limits.$this->orderby.$this->likes.$this->groupby.$this->havings;
}else{
$sql = "select ".$this->sums.$this->counts.$this->maxs.$this->mins.$this->avgs." from ".$this->tables.$this->joins.$this->ons.$this->wheres.$this->limits.$this->orderby.$this->likes.$this->groupby.$this->havings;
}
return $this->dbs->query($sql)->fetchAll(PDO::FETCH_ASSOC);
}
// 删除
public function del($id){
$sql = "delete from ".$this->tables." where id =".$id;
$res = $this->dbs->exec($sql);
return $res;
}
// 改
public function update($data,$id){
foreach ($data as $key => $value) {
@$str .= " ".$key." = "."'".$value."'"." , ";
}
$strs = substr($str,"0","-2");
$sql = "update ".$this->tables." set ".$strs." where id = ".$id;
return $this->dbs->exec($sql);
}
// 增
public function add($data){
$v = array_values($data);
$k = array_keys($data);
$k = implode(",",$k);
$v = implode("','",$v);
$v = "'".$v."'";
@$sql = "insert into"." ".$this->tables."(".$k.")".values."(".$v.")";
return $this->dbs->exec("$sql");
}
}
?>
PDO简单的DB类封装的更多相关文章
- Python一个简单的数据库类封装
#encoding:utf-8 #name:mod_db.py '''使用方法:1.在主程序中先实例化DB Mysql数据库操作类. 2.使用方法:db=database() db.fet ...
- Discuz!数据库操作DB类和C::t类介绍
类定义文件 DB类: 文件\source\class\class_core.php class DB extends discuz_database {} discuz_database类定义 文件\ ...
- 封装自己的DB类(PHP)
封装一个DB类,用来专门操作数据库,以后凡是对数据库的操作,都由DB类的对象来实现.这样有了自己的DB类,写项目时简单的sql语句就不用每次写了,直接调用就行,很方便! 1.封装一个DB类.一个类文件 ...
- nodejs操作mongodb数据库封装DB类
这个DB类也算是我经历了3个实际项目应用的,现分享出来,有需要的请借鉴批评. 上面的注释都挺详细的,我使用到了nodejs的插件mongoose,用mongoose操作mongodb其实蛮方便的. 关 ...
- 封装DB类
封装DB类 一般一个类单独书写在一个Php文件中,为了见名知意,会对文件名有一个规范:类名.class.php 第1步: 创建DB类 第2 步: 属性设计 第3步: 初 ...
- C++基础——类封装简单示例
一.前言 在IC前端设计/验证领域,只会HDL远远不够.目前大多数项目使用已开发好的系统架构和IP Core,因此设计部分的工作量慢慢向系统集成和验证方向转移.而在集成和验证过程中,往往以各种脚本和面 ...
- nodejs mongodb 数据库封装DB类 -转
使用到了nodejs的插件mongoose,用mongoose操作mongodb其实蛮方便的. 关于mongoose的安装就是 npm install -g mongoose 这个DB类的数据库配置是 ...
- python+selenium之自定义封装一个简单的Log类
python+selenium之自定义封装一个简单的Log类 一. 问题分析: 我们需要封装一个简单的日志类,主要有以下内容: 1. 生成的日志文件格式是 年月日时分秒.log 2. 生成的xxx.l ...
- Python之自定义封装一个简单的Log类
参考:http://www.jb51.net/article/42626.htm 参考:http://blog.csdn.net/u011541946/article/details/70198676 ...
随机推荐
- 理解urllib、urllib2及requests区别及运用
urllib and urllib2 区别 –博主提示:下面的是python2中的用法,python3需要做出相应修改. urllib和urllib2模块都做与请求URL相关的操作,但他们提供不同的功 ...
- 2 - Rich feature hierarchies for accurate object detection and semantic segmentation(阅读翻译)
Rich feature hierarchies for accurate object detection and semantic segmentation Ross Girshick Jeff ...
- POJ3764
题目 POJ3764 The xor-longest Path 原题传送门 主要思路: 1.求出每个点到根节点(这里是树,所以直接取0)路径上所有权值xor和为d[i],则任意两点间路径xor和则为 ...
- Linux设备驱动程序 之 顺序锁
当要保护的资源很小,很简单,会频繁的被访问而且写入访问很少的且必须快速时(即读不允许让写饥饿),就可以使用顺序锁(seqlock):从本质上讲,顺序锁会允许读取者对资源的自由访问,但需要读取者检查是否 ...
- Bootstrap4项目开发实战视频教程
一.企业网站项目 课件 0.课程简介 1.顶部区域的制作 2.导航区域的制作 3.轮播区域的制作 4.产品区域的制作 5.最新资讯区域的制作 6.底部区域的制作 二.化妆品网站项目 1.项目初始化_导 ...
- 限定某个业务控制方法,只允许GET或POST请求方式访问
可以在业务控制方法前,指明该业务控制方法只能接收GET或POST的请求 @Controller @RequestMapping(value="/user") public clas ...
- easyUI之Messager(消息窗口)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...
- document.querySelectorAll遍历
document.querySelectorAll兼容性良好,在之前的项目中就其遍历方式出了错误,先做个小结: 1.for循环 传统遍历方法 for(var i= 0; i< document. ...
- powershell自动添加静态IP
声明:其中脚本有参考其他作者,由于当时参考仓促,未能把作者一一列出,有机会会再找出原作者文件链接并附上,请见谅 参考: https://ss64.com/nt/netsh.html https://w ...
- 【分布式事务】使用atomikos+jta解决分布式事务问题
一.前言 分布式事务,这个问题困惑了小编很久,在3个月之前,就间断性的研究分布式事务.从MQ方面,数据库事务方面,jta方面.近期终于成功了,使用JTA解决了分布式事务问题.先写一下心得,后面的二级提 ...