zoukankan      html  css  js  c++  java
  • PDO封装增删改查

    <?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");
    }

    }
  • 相关阅读:
    vue-cli模拟后台数据交互
    打卡谱
    前端面试题
    ps简单切图
    常见浏览器兼容性问题与解决方案
    扯淡 测量体质公式 哈哈哈哈会不会封我号
    JAVASCRIPT 转换字符串杂记
    日记(六)
    gitshell 基础操作
    gitshell fork别人项目
  • 原文地址:https://www.cnblogs.com/maohongli/p/11193139.html
Copyright © 2011-2022 走看看