我试图对从SQL获取的数据进行json_encode,但是,当我将其存储到PHP数组中时,它没有返回任何值。我是否缺少任何代码,或者数组是否有限制?
我在循环内尝试了json_encode(),但我需要用角度4来读取从php文件编码的json文件。另外,我尝试检查循环是否正常工作,并从SQL提取了56行,但是当我将其放入PHP数组时,该数组显示空白的PHP文件。
/location.php(TRY1)
$sql = "SELECT * FROM api_locations";
$result = mysqli_query($con, $sql);
$outp = array();
if(mysqli_num_rows($result) > 0)
{
while($data = $result->fetch_assoc()){
$location = $data['loc_name'];
$feature = $data['feature'];
$lat = $data['lat'];
$lng = $data['lng'];
$desc = $data['description'];
$hrs = $data['hrs'];
$min = $data['min'];
$rating = $data['rating'];
$max_b = $data['budget_maximum'];
$min_b = $data['budget_minimum'];
$outp[] = array(
"location" => $location,
"feature" => $feature ,
"lat" => $lat ,
"lng" => $lng ,
"description" => $desc ,
"hrs" => $hrs ,
"min" => $min ,
"rating" => $rating ,
"budget_maximum" => $max_b ,
"budget_minimum" => $min_b ,
);
}
echo json_encode($outp);
}
或者我用另一种方式做了while循环:
while ($data = mysqli_fetch_assoc($result)) {
$outp[] = $data;
}
注意:执行json_encode时,$ outp仍然为空。
我希望输出会是这样。
{“ id”:“ 1”,“ loc_name”:“ Robinsons 玉兰”,“ lat”:“ 123.1321”,“ lng”:“ 12.213”,“功能”:“购物”,“说明”:“快乐” 放置于 shop“,” hrs“:” 1“,” min“:” 30“,” rating“:” 4“,” budget_maximum“:” 5000“,” budget_minimum“:” 100“}
...
{“ id”:“ 56”,“ loc_name”:“ CHICHIHC”,“ lat”:“ 213.1213”,“ lng”:“ 12.23131”,“ feature”:“ Entertainment”,“ description”:“ Good制作的地方 kalokohan“,” hrs“:” 1“,” min“:” 30“,” rating“:” 4“,” budget_maximum“:” 100“,” budget_minimum“:” 50“}
但是实际输出为空白。
我也尝试这样做
$sql = "SELECT *
FROM api_locations WHERE id<=9";
这有效。完全!但是当它超过9时仍然无法正常工作。