json编码显示" null"身体上

时间:2016-04-18 12:12:06

标签: php android json

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

    $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);

1 个答案:

答案 0 :(得分:0)

你应该将mysqli_set_charset($con,"utf8");移动到连接之下,它在运行查询后迟到了

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