码迷,mamicode.com
首页 > 其他好文 > 详细

PDO封装增删改查

时间:2019-07-16 10:29:21      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:修改   lod   root   查询   UNC   roo   into   key   class   

<?php
class db{

public $table=null;
public $pdo;
public $where=null; //where 条件
public $field=null; //要查询的条件

public function __construct()
{
$this->pdo=new PDO("mysql:host=127.0.0.1;dbname=1611b","root","root");
}

public function fetch(){
return $this->pdo->query("select * from $this->table $this->where")->fetch(PDO::FETCH_ASSOC);

}

public function table($table){
$this->table=$table;
return $this;
}

public function where($where){
$str="where ";
foreach ($where as $k=>$v){
$str.=$k."="."‘".$v."‘". " and " ;
}
$this->where=rtrim($str," and ");
return $this;
}


public function insert($data){
$k=array_keys($data);
$k=implode($k,‘,‘);
$str="";
foreach ($data as $key=>$value){
$str.=","."‘".$value."‘";
}
$str=substr($str,1);
return $this->pdo->exec("insert into $this->table ($k) values ($str)");


}


public function delect($id){
$str=‘‘;
$str1=‘‘;
foreach ($id as $k=>$v){
$str.=$k;
foreach ($v as $kk=>$vv){
$str1.=‘,‘.$vv;
}
}

$str2=substr($str1,1);

$ids=‘where ‘.$str.‘ in ‘.‘(‘.$str2.‘)‘;
return $this->pdo->exec("delete from $this->table $ids");
}



function update($res){
//修改
$str=‘‘;
foreach ($res as $k=>$v){
$str.=‘,‘.$k.‘=‘."‘".$v."‘";
}
$str=substr($str,1);
return $this->pdo->exec("update $this->table set $str $this->where");
}

}

PDO封装增删改查

标签:修改   lod   root   查询   UNC   roo   into   key   class   

原文地址:https://www.cnblogs.com/maohongli/p/11193139.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!