I'm trying to output a JSON feed in PHP and I keep having an issue where the second result in the JSON feed includes the results of the first as well.
Source below, and output:
Source
function fetch_tour_list($tourID) {
include('../inc/conn.php');
$query = mysqli_query($conn,"SELECT * FROM ticket_tour_dates WHERE TourID = $tourID");
while($result = mysqli_fetch_array($query)) {
$date['date'] = $result['Date'];
$venueID = $result['VenueID'];
$venue_query = mysqli_query($conn,"SELECT * FROM ticket_venues WHERE ID = $venueID");
while($venue_result = mysqli_fetch_array($venue_query)) {
$venue['id'] = $venue_result['ID'];
$venue['name'] = $venue_result['Name'];
$venue['location'] = $venue_result['Location'];
$venue['latitude'] = $venue_result['Lat'];
$venue['longitude'] = $venue_result['Long'];
$venues[] = $venue;
}
$date['venue'] = $venues;
$dates[] = $date;
}
echo json_encode($dates);
mysqli_close($conn);
}
Output
[{"date":"2013-07-29","venue":[{"id":"1","name":"The Gramercy","location":"New York City","latitude":"50.00000000","longitude":"50.00000000"}]},{"date":"2013-08-02","venue":[{"id":"1","name":"The Gramercy","location":"New York City","latitude":"50.00000000","longitude":"50.00000000"},{"id":"2","name":"The Troubadour","location":"Chicago","latitude":"20.00000000","longitude":"25.00000000"}]}]
Cameron Skinner
55.3k3 gold badges70 silver badges86 bronze badges
asked Jul 21, 2013 at 4:59
Jackolai
1,3671 gold badge8 silver badges11 bronze badges
-
1Don't make queries like that in nested loops. Get all the data in one JOIN query.Barmar– Barmar2013年07月21日 05:06:41 +00:00Commented Jul 21, 2013 at 5:06
1 Answer 1
Add the following line before the inner while loop:
$venues = array();
answered Jul 21, 2013 at 5:03
jburns20
3,1572 gold badges28 silver badges32 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-php