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

1 Answer 1

2

Add the following line before the inner while loop:

$venues = array();

Your Answer

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.