1

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
1
  • 1
    Don't make queries like that in nested loops. Get all the data in one JOIN query. Commented Jul 21, 2013 at 5:06

1 Answer 1

2

Add the following line before the inner while loop:

$venues = array();
answered Jul 21, 2013 at 5:03
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.