2015-03-05 07:19:17
0
i am trying to fetch data from MySQL and show it in JSON format
This is the partial PHP code$sql = "SELECT item, cost, veg, spicy_level FROM food1";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
echo json_encode($row),"
";}
?>
i am getting output as
{"item":"dosa","cost":"20","veg":"0","spicy_level":"1"}
{"item":"idli","cost":"20","veg":"0","spicy_level":"2"}
but i need it as
food1:[
{"item":"dosa","cost":"20","veg":"0","spicy_level":"1"},
{"item":"idli","cost":"20","veg":"0","spicy_level":"2"}
]
can anyone please guide me?
i think what i am getting is in object format and i need output in array format i.e. with [ & ].
very new to this json and php.