时间:2021-07-01 10:21:17 帮助过:4人阅读
类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教程有兴趣的朋友有所帮助。