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

2022-10-11 22:00:20 198 0
魁首哥

链接类

 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结果

收藏
分享
海报
0 条评论
198
上一篇:PHP的OpenSSL加密扩展学习(三):证书操作 下一篇:5分钟搭建起apache+php+mysql开发环境

本站已关闭游客评论,请登录或者注册后再评论吧~

忘记密码?

图形验证码