<?phpphp
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;mysql
private $havings = null;sql
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;
}數據庫
//條件fetch
public function where($k,$v){
$this->wheres = " where ".$k.'='.$v;
return $this;
}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;
}get
// 查詢語句
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);
}it
// 刪除
public function del($id){
$sql = "delete from ".$this->tables." where id =".$id;
$res = $this->dbs->exec($sql);
return $res;
}io
// 改
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);table
}
// 增
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");
}
} ?>