我的数据库(Mysql)
product_name
ID product_name qty
1 item a 5
2 item b 4
3 item c 3我的php代码
<?php
include("connect.php");
$query="select*from product_name";
$result = mysqli_query($db, $query) or die("Error in Selecting " .mysqli_error($db));
while ($row=mysqli_fetch_assoc($result)){
$arrey[]=$row;
}
echo json_encode($arrey);
?>输出为
[{
"id": "1",
"productname": "item a",
"qty": "5"
}, {
"id": "2",
"productname": "item b",
"qty": "4"
}, {
"id": "3",
"productname": "item c",
"qty": "3"
}]我不得不像贝罗一样面对数据。
{
"status": "true",
"message": "Data fetched successfully!",
"data": [{
"id": "1",
"productname": "item a",
"qty": "5"
},
{
"id": "2",
"productname": "item b",
"qty": "4"
},
{
"id": "3",
"productname": "item c",
"qty": "3"
}
]
}该怎么做呢?
发布于 2019-05-13 03:17:26
您应该将数组添加到头数组中
$myArray = ['status' =>"true",
"message"=> "Data fetched successfully!",
'data' =>$arrey];。
<?php
include("connect.php");
$query="select*from product_name";
$result = mysqli_query($db, $query) or die("Error in Selecting " .mysqli_error($db));
while ($row=mysqli_fetch_assoc($result)){
$arrey[]=$row;
}
$myArray = ['status' =>"true",
"message"=> "Data fetched successfully!",
'data' =>$arrey];
echo json_encode($myArray);
?>https://stackoverflow.com/questions/56102605
复制相似问题