在做小程序后台接口时需要将数据库查询结果返回为Json格式
现记录如下:
$conn = mysqli_connect('localhost','root','root','test')or ('error');
mysqli_query($conn,"set names utf8");
$result=mysqli_query($conn,"select * from centernav");
$jarr = array();
while ($rows=mysqli_fetch_array($result,MYSQL_ASSOC)){
$count=count($rows);//不能在循环语句中,由于每次删除 row数组长度都减小
for($i=0;$i<$count;$i++){
unset($rows[$i]);//删除冗余数据
}
array_push($jarr,$rows);
}
echo $str=json_encode($jarr);//将数组进行json编码
//print_r($jarr);//查看数组

目前评论:0