php封装mysql类

限于喜欢 提交于 2019-12-23 02:43:23

php封装myslq类(仅供参考)

<?php


namespace common;
use PDO;

class DB
{
//    public $config;
    public $mysql;
    public $table;
    public $where;
    public $pdo;
    public $getresult;
    /**
     * @content mysqli 数据库链接方式
     */
    public function connect()
    {
        $config = config()['db'];
        $this->mysql = mysqli_connect($config['host'],$config['user'],$config['pwd'],$config['databasename']);
        return $this->mysql;
    }

    /**
     * @param $param
     * @return $this
     * 设置表名
     */
    public function table($param)
    {
        $this->table = $param;
        return $this;
    }

    /**
     * @param $where
     * @return $this
     * @content 查询条件 mysqli
     */ 
    public function where($key,$where,$val)
    {
        $this->where = ' where '.$key.$where."'$val'";
        return $this;
    }

    /**
     * 查询所有的mysqli方式
     */
    public function all()
    {
        $sql = "select * from ".$this->table.$this->where;
        $res = mysqli_query($this->connect(),$sql);
        $res = mysqli_fetch_all($res,1);
        return $res;
    }
    /**
     * @return array|null
     * 查询单条数据 mysqli方式
     */
    public function get()
    {
        $sql = "select * from ".$this->table.$this->where;
        $res = mysqli_query($this->connect(),$sql)->fetch_assoc();
        return $res;
    }

    /**
     * @content mysqli 删除处理
     */
    public function delete()
    {
        $sql = 'delete from '.$this->table.$this->where;
        $res = mysqli_query($this->connect(),$sql);
        return $res;
    }

    /**
     * @onotent mysqli  添加方法
     */
    public function insert($data)
    {
        $k_str = '';
        $v_str = '';
        foreach ($data as $key => $val) {
            if (empty($val)) {
                echo "<script>alert('字段值不能为空');</script>";die;
            }
            //$key的值是每一个字段s一个字段所对应的值
            $k_str .= $key . ',';
            $v_str .= "'$val',";
            $key_str = trim($k_str, ',');
            $val_str = trim($v_str, ',');
            //判断数据是否为空
            $sql = "insert into ".$this->table ." ($key_str) values ($val_str)";
        }
        $res = mysqli_query($this->connect(),$sql);
        return $res;
    }

    /**
     * @content mysqli 修改封装方法
     */
    public function update($data)
    {
        $str = '';
        foreach ($data as $key=>$val){
            $str .= "$key = '$val',";
        }
        $strs = trim($str,',');
        $sql = "update ".$this->table." set ".$strs.$this->where;
        $res = mysqli_query($this->connect(),$sql);
        return $res;
    }


    public function count()
    {
        $sql = "select count(*) as count from ".$this->table . $this->where;
        $res = mysqli_query($this->connect(),$sql);
        $data = mysqli_fetch_assoc($res);
        return $data;
    }

    public function paginate($p,$basr)
    {
        // 计算全部数据数
        $number = $this->table('user')->count();
        // 计算页数
        $count = ceil($number['count'] / $basr);
        // 页码
        $path_str = "<ul class='zc_ch'>";
        for($i = 1;$i <= $count; $i++){
            if($i == $p){
                $path_str .= "<li><span>".$i."</span></li>";
            }else{
                $path_str .= "<li><a href='#?p=".$i."'>".$i."</a></li>";
            }
        }
        $path_str .= "</ul>";
        // 本页显示的数据
        $num = ($p-1)*$basr;
        $sql = "select * from ".$this->table." limit ".$num.','.$basr;
        $res = mysqli_query($this->connect(),$sql);
        $data = mysqli_fetch_all($res,MYSQLI_ASSOC);
        return [$path_str,$data];
    }

    /**
     * @param $table
     * @param $inid
     * @param $outid
     * @return array
     * 双表联查
     */
    public function join($table,$inid,$outid)
    {
        $sql = "select * from ".$this->table.' join '.$table.' on '.$inid.'='.$outid;
        $res = mysqli_query($this->connect(),$sql);
        $data = mysqli_fetch_all($res,1);
        return $data;
    }
    
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!