mysql class comments throughout

WBOY
Release: 2016-08-08 09:32:17
Original
827 people have browsed it

Profile Section

Copy after login

Class
conn = new mysqli($host,$username,$passwd);
        $this->selectDb($dbname);
        $this->coding($cod);
    }

    //设置编码格式
    public function coding($cod)
    {
        mysql_query("SET CHARACTER SET ".$cod);
    }

    //读取某个库
    public function selectDb($dbname)
    {
        mysql_select_db($dbname,$this->conn);
    }

    //要查询什么字段 这个事查询的最后一步
    public function select($field="*")
    {
        $this->sql = "select ".$field." from ".$this->dataTable.$this->where.$this->limit;

    }

    //要使用哪个表
    public function table($tableName)
    {
        $this->dataTable = $tableName;
        return $this;
    }

    //执行操作
    public function query()
    {
        $res = mysql_query($this->sql);

        //是资源类型代表查询 返回结果数组
        if(is_resource($res))
        {
            //将返回二维数组
            while($row = mysql_fetch_assoc($res))
            {
                $arr[] = $row;
            }
            //返回一维数组
            foreach($arr as $key=>$value)
            {
                $ke = $value;
            }
            return $ke;
        }

    }

    //取得几个值 limit
    public function limit($limit)
    {
        $this->limit = ' limit '.$limit;
        return $this;
    }

    //where条件
    public function where($where)
    {
        $this->where = ' where '.$where;
        return $this;
    }

    //insert
    public function insert($insert)
    {
        //如果是索引数组
        if($this->is_assoc($insert))
        {
            //获取数组的key 也就是字段值
            $key = array_keys($insert);
            $value = array_values($insert);
            for($i=0,$j=0;$isql = "insert into ".$this->dataTable."($col) values($value)";

        }else{
            //如果不是索引数组 那么直接将数组转化为字符串
            $value = implode(',',$insert);
            $this->sql = "insert into ".$this->dataTable." values($value)";
        }
    }

    //修改
    public function update($update)
    {
        foreach($update as $key=>$value)
        {
            $data.=$key."=".$value.',';
        }
        $values = rtrim($data,',');
        $this->sql = "UPDATE ".$this->dataTable." SET $values ".$this->where;
    }

    //删除
    public function delete()
    {
        $this->sql = "DELETE FROM ".$this->dataTable.$this->where;
    }

    //判断是否为索引数组
    public function is_assoc($array) {
        if(is_array($array)) {
            $keys = array_keys($array);
            return $keys != array_keys($keys);
        }
        return false;
    }

}


//$b = array('CategoryID'=>9,'Name'=>11,'Description'=>22,'Priority'=>35);
//$b = array(4,5,6,7);
//$a = new MySqli();
//查询
//$sql = $a->table("guagua_category")->where('CategoryID=1')->limit(3)->select();
//$arr = $a->query();

//插入
//$sql = $a->table("guagua_category")->insert($b);
//$a->query();

//修改
//$sql = $a->table("guagua_category")->where("CategoryID=1")->update($b);
//$a->query();
//删除
//$sql = $a->table("guagua_category")->where("CategoryID=9")->delete();
//$a->query();
Copy after login

The above introduces the mysql class and comments throughout the article, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!