json_encode不返回任何内容 [英] json_encode not returning anything
本文介绍了json_encode不返回任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将MYSQL表数据转换为JSON.我正在尝试使用json_encode().但这是行不通的.它不返回任何东西.我已经检查了控制台,甚至没有抛出任何错误.我想念什么?
I am trying to convert my MYSQL table data into JSON. I am trying with json_encode(). But it does not work. It does not return anything. I have checked the console,doesn't even throw any errors. What am i missing?
<?php
//open connection to mysql db
$connection = mysqli_connect("localhost","root","","maps") or die("Error " . mysqli_error($connection));
//fetch table rows from mysql db
$sql = "select * from locations";
$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));
//create an array
$emparray[] = array();
while($row =mysqli_fetch_assoc($result))
{
$emparray[] = $row;
}
echo json_encode($emparray);
//close the db connection
mysqli_close($connection);
?>
推荐答案
尝试一下
while ( $row = $result->fetch_assoc() ){
$emparray[] = json_encode($row);
}
echo json_encode( $emparray );
或
while($row =mysqli_fetch_assoc($result))
{
$emparray[] = json_encode($row);
}
echo json_encode($emparray);
或
$emparray = $result->fetch_all( MYSQLI_ASSOC );
echo json_encode( $emparray );
代替
while($row =mysqli_fetch_assoc($result))
{
$emparray[] = $row;
}
echo json_encode($emparray);
这篇关于json_encode不返回任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文