我的数据库(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"
}
]
}
如何做到?
答案 0 :(得分:0)
您应将您的数组添加到标头数组
$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);
?>