Is it possible to place the rows from a query into JSON using php? I'm guessing it is, but I have no clue how to go about it.
The goal is to search the database based on an id and get the colour and number from all entries who have the right id. Those colours and numbers then need to be placed into JSON, something similar to this:
$jsonData = '{
"land1":{ "colour":"'.$colour.'", "number":"'.$number.'" }
"land2":{ "colour":"'.$colour.'", "number":"'.$number.'" }
ETC..
}';
One for each entry received from the query? Is there a way to do this all at once, or do I have to loop and do one entry each loop?
Simple create an array containing all the results of your query and then convert the array to a JSON string using json_encode()
$json_data = array();
while ( $row = fetch using mysqli or PDO ) {
$json_data[] = $row;
}
echo json_encode($json_data);