json编码显示“;空”;身体上


json encode displaying "null" on body

我的代码如下所示,如果我回显它,我可以很好地显示正文,但当我尝试编码它时,它返回空

    $con=mysqli_connect("localhost", "root","","eservices"); 
    $sql="select * from news order by id DESC";
    $result=mysqli_query($con,$sql);
    mysqli_set_charset($con,"utf8");
    $news=array();
    while($row=mysqli_fetch_array($result)){
    array_push($news, array("headline"=>$row[2],"body"=>$row[3]));
    }
    echo json_encode(array("news_response"=>$news));
    mysqli_close($con);

您应该将mysqli_set_charset($con,"utf8");移动到连接下,以便在运行查询后延迟执行

 $con=mysqli_connect("localhost", "root","","eservices");
 mysqli_set_charset($con,"utf8");