十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
这篇文章给大家介绍使用PHP怎么封装一个mysqli数据库操作类,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
创新互联公司主要从事网站设计制作、成都做网站、网页设计、企业做网站、公司建网站等业务。立足成都服务新晃,10多年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18980820575db_conn= mysqli_connect($this->db_url,$this->db_username,$this->db_userpassword,$this->db_name); if (!$this->db_conn) { echo "连接 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怎么封装一个mysqli数据库操作类就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。