本文实例讲述了PHP封装的mysqli数据库操作类。分享给大家供大家参考,具体如下:

由于PHPMySQL操作在PHP5.4以下被弃用而推荐使用mysqli(MySQL Improvement),这里是在我原有的MySQL数据库操作类的基础上加以改进而编写出的MySQLI数据库操作类,整体代码与MySQL数据库操作类大致相同,直接实例化db_class即可

<"连接 MySQL 失败: " . mysqli_connect_error();
    }
    }
    public function __construct($db_url,$db_username,$db_userpassword,$db_name){ //构造方法赋值
    $this->db_url=$db_url;
        $this->db_username=$db_username;
        $this->db_userpassword=$db_userpassword;
        $this->db_name=$db_name;
        $this->db_order="";
        $this->db_limit="";
        $this->db_getconn();
    mysqli_query($this->db_conn,'set names utf8');
    }
    public function db_settablename($db_tablename){ //设置表名
        $this->db_tablename=$db_tablename;
    }
    public function db_setorder($str){ //排序操作
        $this->db_order="order by $str";
    }
    public function db_setlimit($start,$end){ //分页操作
     $this->db_limit="limit $start,$end";
    }
    public function db_select($typearr="",$where=""){ //查询操作
        if(empty($typearr)){
            $typearr="*";
        }
        else{
            $typearr=implode(",",$typearr);
        }
        if(empty($where)){
            $where="";
        }else{
            $where="where ".$where;
        }
        $arr=array();
        $sql="select $typearr from $this->db_tablename $where $this->db_order $this->db_limit ";
        $result = mysqli_query($this->db_conn,$sql);
        while($row = $result->fetch_array()) {
            $arr[]=$row;
        }
        return $arr;
    }
    public function db_update($typearr,$valuearr,$where=""){ //更新操作
    $sql="";
        if(empty($where)){
            $where="";
        }else{
            $where=" where ".$where;
        }
        $sql.="update $this->db_tablename set ";
         foreach ($typearr as $key=>$value){
             if(count($typearr)-1==$key){
                 $sql.=$value."='".$valuearr[$key]."'";
             }else{
                 $sql.=$value."='".$valuearr[$key]."'".",";
             }
         }
        $sql.=$where;
    mysqli_query($this->db_conn,$sql);
    }
    public function db_delete($typestr,$valuestr){ //删除操作
    $sql="delete from $this->db_tablename WHERE $typestr=$valuestr";
    mysqli_query($this->db_conn,$sql);
    }
    public function db_insert($typearr,$valuearr){ //插入操作
        $sql="insert into $this->db_tablename(".implode(",", $typearr).") values(".implode(",", $valuearr).")";
    mysqli_query($this->db_conn,$sql);
    }
    public function __destruct(){ //析构方法关闭连接
        mysqli_close($this->db_conn);
    }
}

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php+mysqli数据库程序设计技巧总结》、《php面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》、《PHP基本语法入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

标签:
PHP,mysqli,数据库,操作类

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com

评论“PHP封装的mysqli数据库操作类示例”

暂无“PHP封装的mysqli数据库操作类示例”评论...