欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PDO简单的DB类封装

程序员文章站 2022-06-24 09:20:23

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


}
?>