首页 > 作文

PDO简单的DB类封装

更新时间:2023-04-07 08:37:48 阅读: 评论:0

<?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 = ” orde等到烟火清凉歌词r 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 = “lect “.$this->fields.” from “.$this->tables.$this->joins.$this->ons.$this->wheres.$thi宇宙之前是什么s->limits.$this->orderby.$this->likes.$this->groupby;
}el{
$sql = “lect “.$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 &#zhaopengyou8220;.$this->tables.” t “.$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 = “inrt 好段大全into”.” “.$this->tables.”(“.$k.”)”.values.”(“.$v.”)”;
return $this->dbs->exec(“$sql”);
}

}
?>

本文发布于:2023-04-07 08:37:47,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/46be627130a31f8279fc80d2278d2234.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:PDO简单的DB类封装.doc

本文 PDF 下载地址:PDO简单的DB类封装.pdf

标签:链接   条件   最大值   平均值
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图