mysql类 通篇注释

WBOY
发布: 2016-08-08 09:32:17
原创
843 人浏览过

配置文件部分

登录后复制

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();
登录后复制

以上就介绍了mysql类 通篇注释,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!