您的位置 首页 php

php Mysqli 重新构架遍历结果 转Json 后端

链接类

 <?php
    header("content-type:application/html;charset=UTF-8");
    class Mysqldb{
        //创建属性
        private $conn;

        public function __construct($host='xxxxxx',$user='xxx',$pwd='xxx',$database='xx'){
            $this->conn=mysqli_connect($host,$user,$pwd,$database);
            if($this->conn){
                $this->conn->set_charset('utf8');
            }
        }

        public function simple_query($sql){
            $result=mysqli_query($this->conn,$sql);
            return $result;
        }

        public function query($sql){
            $result=mysqli_query($this->conn,$sql);
            $arr=array();
            while ($row = $result->fetch_assoc()){
                array_push($arr,$row);
            }
            mysqli_free_result($result);
            return json_encode($arr);
        }
        public function update($sql){
            $result=mysqli_query($this->conn,$sql);
            $arr=array();
            if($result){
                $arr['flag']=true;
                $arr['msg']='Sucessful';
            }else{
                $arr['flag']=false;
                $arr['msg']='Failure';
            }
            return json_encode($arr);
        }
        public function __destruct(){
            mysqli_close($this->conn);
            $this->conn=null;
        }
    }
?>
  

php 后端查询

     $sql = "
    select
    user_username, 
    user_en
    from
    tbl_user
";
$result=$sqls->simple_query($sql);
$arr=array();
$inum=0;
while ($row = $result->fetch_assoc()){
    $arr[$inum]['user_username'] = $row['user_username'];
    $arr[$inum]['user_en'] = $row['user_en'];
    $inum++;
}
mysqli_free_result($result);
echo json_encode($arr);  

postman结果

文章来源:智云一二三科技

文章标题:php Mysqli 重新构架遍历结果 转Json 后端

文章地址:https://www.zhihuclub.com/76994.shtml

关于作者: 智云科技

热门文章

网站地图