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 $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

自主封装PHP ORM框架,面向对象的PDO数据库操作,API框架,支持Get/Post/Put/Delete多种请求方式。 代码示例: <?php use Models\User; require '../application.php'; require '../loader-api.php'; //适合查询,如:获取用户列表或者单个用户信息 execute_request(HttpRequestMethod::Get, function() { $action = request_action(); //判断是否存在 if ($action == 1) { list($type, $value) = filter_request(array( request_int('type', 1, 2, 3), //1.用户名 2.邮箱 3.手机号 request_string('value'))); $type_field_map = array( 1 => User::$field_username, 2 => User::$field_email, 3 => User::$field_phone ); if ($type == 2 && !is_email($value) || $type == 3 && !is_mobilephone($value)) { die_error(USER_ERROR, $type_field_map[$type]['name'] . '格式无效'); } $user = new User(); $user->set_where_and($type_field_map[$type], SqlOperator::Equals, $value); $result = $user->exists(create_pdo()); echo_result($result ? 1 : 0); //存在返回1,不存在返回0 } //查询单条信息 if ($action == 2) { list($userid) = filter_request(array( request_userid())); //查询单条数据 $user = new User($userid); //set_query_fields可以指定查询字段,下面两种写法均可 //$user->set_query_fields('userid, username, email'); //$user->set_query_fields(array(User::$field_userid, User::$field_username, User::$field_email)); //还可设置where条件进行查询 //$user->set_where_and(User::$field_status, SqlOperator::Equals, 3); //$user->set_where_and(User::$field_truename, SqlOperator::IsNullOrEmpty); //$user->set_where_and(User::$field_age, SqlOperator::In, array(27, 29)); //$user->set_where_and(User::$field_regtime, SqlOperator::LessThan, '-6 month'); //创建数据库连接 $db = create_pdo(); $result = $user->load($db, $user); //也可以用Model的静态方法 //$result = Model::load_model($db, $user, $user); if (!$result[0]) die_error(PDO_ERROR_CODE, '获取用户信息时数据库错误'); if (!$user) di
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值