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 ...
随机推荐
- Django从Models 10分钟建立一套RestfulApi
目录 Django从Models 10分钟建立一套RestfulApi Django从Models 10分钟定制一个Admin后台 简介 Django是一套完善而强大的web开发框架, 结合Djang ...
- apache和tomcat有什么不同
前言 apache是web服务器,tomcat是应用(java)服务器,它只是一个servlet容器,可以认为是apache的扩展,但是可以独立于apache运行. 换句话说,apache是一辆卡车, ...
- 面试题_Spring基础篇
Spring基础题 1. 什 么 是 Spring? Spring 是 个 java 企 业 级 应 用 的 开 源 开 发 框 架 .Spring 主 要 用 来 开 发 Java 应 用 , 但 ...
- mysql 日期字符串互转
字符串转日期select str_to_date('2008-4-2 15:3:28','%Y-%m-%d %H:%i:%s');select str_to_date('2008-08-09 08:9 ...
- 使用io/ioutil进行读写文件
读文件: package main import ( "fmt" "io/ioutil" ) func main() { b, err := ioutil.Re ...
- MongoDB系列一:MongoDB文档型数据库特点介绍
一.MongoDB数据库的存储特点: 1.MongoDB是文档型数据库,存储的是文档(将json二进制化为Bson存储): 2.存储的每篇文档都可以有自己独特的存储结构,颠覆了传统数据库必须按照表和列 ...
- 手把手教你在Linux系统下安装MySQL
在CentOS中默认安装有MariaDB,这个是MySQL的分支,但为了需要,还是要在系统中安装MySQL,而且安装完成之后可以直接覆盖掉MariaDB. 1. 下载并安装MySQL官方的 Yum R ...
- 性能测试 | Linux线程数、进程查询
I. 最大值查询 1. 最大进程数查询 1 cat /proc/sys/kernel/pid_max 个人阿里云机器(单核2g)上输出如下 1 32768 2. 最大线程数查询 1 cat /proc ...
- python 通过 实例方法 名字的字符串调用方法
方式1 - 反射 hasattr 方法 判断当前实例中是否有着字符串能映射到的属性或者方法, 一般会在 getattr 之前作为判断防止报错 getattr 方法 获取到当前实例中传入字符串映射到的 ...
- 014-数据结构-树形结构-基数树、Patricia树、默克尔树、梅克尔帕特里夏树( Merkle Patricia Tree, MPT)
一.基数树 Radix树,即基数树,也称压缩前缀树,是一种提供key-value存储查找的数据结构.与Trie不同的是,它对Trie树进行了空间优化,只有一个子节点的中间节点将被压缩.同样的,Radi ...