博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PDO简单的DB类封装
阅读量:5144 次
发布时间:2019-06-13

本文共 3304 字,大约阅读时间需要 11 分钟。

<?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 $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 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;
}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;
}
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");
}

}
?>

转载于:https://www.cnblogs.com/wenjixin/p/11206969.html

你可能感兴趣的文章
运行一个窗体直接最大化并把窗体右上角的最大化最小化置灰
查看>>
Android开发技术周报 Issue#80
查看>>
hadoop2.2.0+hive-0.10.0完全分布式安装方法
查看>>
WebForm——IIS服务器、开发方式和简单基础
查看>>
小实验3:实现haproxy的增、删、查
查看>>
Angular中ngModel的$render的详解
查看>>
读《格局》| 未到年纪的真理
查看>>
[转]《城南旧事》里的《送别》
查看>>
07动手动脑
查看>>
django知识点总结
查看>>
C++ STL stack、queue和vector的使用
查看>>
OAuth2 .net MVC实现获取token
查看>>
java中XML操作:xml与string互转、读取XML文档节点及对XML节点增删改查
查看>>
Nginx多域名配置
查看>>
使用Reporting Services时遇到的小问题
查看>>
传递事件和传递命令系统···
查看>>
约瑟夫问题
查看>>
Arduino 报错总结
查看>>
树莓派Android Things物联网开发:树莓派GPIO引脚图
查看>>
Database、User、Schema、Tables、Col、Row
查看>>