这是我在网上的mysql数据库
This is my database on the web
这是访问数据库以获取JSON文件的PHP文件,首先我选择表中的所有“类别”单元格,然后遍历该类别,然后遍历该类别的其他列,然后进行分配到$ data2数组,然后将$ data2和category分配给br一个$ data数组,这是我要显示的json。
<?php
header("Access-Control-Allow-Origin: *");
$user = "u348833238_rest"; /* User */
$password = "a!23286029"; /* Password */
$dbname = "u348833238_rest"; /* Database name */
$host = "localhost";
$con = mysqli_connect($host, $user, $password, $dbname);
// Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
// $sel = mysqli_query($con,"select * from restaurant");
// $data = array();
// while ($row = mysqli_fetch_array($sel)) {
// $data[] = array("dishes" => ["name"=>$row['food'], "price"=>$row['price']] , "category"=>$row['category']);
// }
// echo json_encode($data);
$sel = mysqli_query($con,"select distinct category from restaurant");
$data = array();
while ($row = mysqli_fetch_array($sel)) {
$c = $row['category'];
$sel2 = mysqli_query($con,"select * from restaurant where category = $c ");
$data2 = array();
while ($row2 = mysqli_fetch_array($sel2)){
$data2[] = array("name"=>$row2['food'], "price"=>$row2['price']);
}
// echo $data2;
$data[] = array("category"=>$row['category'], "dishes"=>$data2);
}
// echo $data;
echo json_encode($data);
?>
由于属性“ dishes”的数组为空,JSON如何无法正确显示,如下所示:
答案 0 :(得分:0)
我建议您打开错误,以便您查看问题出在哪里。
猜测在这里:
select * from restaurant where category = $c
应该是:
select * from restaurant where category = '$c'