zoukankan      html  css  js  c++  java
  • sql增、删、改、查基础封装类

    <?php
    class DB
    {
    private $link;//私有变量

    /**
    * 构造方法
    * DB constructor.
    * @param $dbname
    * @param $username
    * @param $pwd
    * @param string $host
    */
    public function __construct($dbname,$username,$pwd,$host='127.0.0.1')
    {
    $this->link=new PDO("mysql:host:$host:dbname=$dbname","$username","$pwd");
    $this->link->exec('set names utf8');
    $this->link->setAttribute(PDO::ATTR_ERRMODE,1);
    }

    /**
    * 获取一条数据
    * @param $where  平衡例条件
    * @param $table  要查询的数据表
    * @return int   返回值
    */
    public function getOne($where="1",$table)
    {

    if (empty($table)) {
    return "没有输入数据表";
    die();
    }
    $sql="select * from new where $where";
    $stmt=$this->link->query($sql);
    return $stmt->fetch(2);
    }

    /**
    * 查询多条数据
    * @param string $where 要查询的数据
    * @param $table 要查询的数据表
    * @return string 返回值
    */
    public function getAll($where="1",$table)
    {

    if (empty($table)) {
    return "没有输入数据表";
    die();
    }
    $sql="select * from new where $where";
    $stmt=$this->link->query($sql);
    return $stmt->fetchAll(2);
    }

    /**
    * 删除
    * @param $table 要删除的数据表
    * @param $where 删除的条件
    * @return int|string 返回值
    */
    public function del($table,$where){
    if (count($where)==0 || empty($table)){
    return "没有删除条件";
    }
    $str="";
    foreach ($where as $k=>$v) {
    $str.="`$k`='$v'";
    }
    $sql="delete from `$table` where $str";
    return $this->link->exec($sql);
    }

    /**
    * 添加一条语句
    * @param $table 要添加的数据表
    * @param $array 添加的数据
    * @return int|string 返回值
    */
    public function add($table,$array) {
    if(empty($table)){
    return "没有插入的数据表";
    }
    if(count($array)==0){
    return "没有要插入的数据";
    die();
    }
    $filed="";//用于字段
    $val="";//用于存放数据
    foreach ($array as $k=>$v){
    $filed.="`$k`,";
    $val.="'$v',";
    }
    $filed=substr($filed,0,-1);
    $val=substr($val,0,-1);
    $sql="insert into `$table`($filed) VALUES ($val)";
    return $this->link->exec($sql);
    }

    /**
    * 修改的方法
    * @param $table 要修改的数据表
    * @param $array 修改的数据
    * @param $where 修改的条件
    * @return int|string 返回值
    */
    public function save($table,$array,$where=""){
    if(empty($table)){
    return "没有修改的数据表";
    }
    if(count($array)==0){
    return "没有要修改的数据";
    die();
    }
    if(empty($where)){
    return "没有要修改的条件";
    die();
    }
    $str="";
    foreach ($array as $k=>$v){
    $str.="`$k`='$v',";
    }
    $str=substr($str,0,-1);
    $sql="update `$table` set $str where $where";
    return $this->link->exec($sql);
    }
    public function __destruct()
    {
    // TODO: Implement __destruct() method.
    $this->link="";//关闭连接
    }
    }
    ?>
  • 相关阅读:
    orcal下的中国大陆地热、重力网络数据库(用户网页模块设计)
    学生管理系统毕业论文
    图像效果算法设计—静态效果
    数字温度计(单片机课程设计)
    Orcal下的中国大陆地热、重力网络数据库(数据库模块设计)
    电脑公司财务管理系统(论文+论计)
    明天开始开年会!
    今天北京下雪了!
    三天年会开完了!
    页面元素添加提示(tooltip)
  • 原文地址:https://www.cnblogs.com/liujunyue/p/11320761.html
Copyright © 2011-2022 走看看