Ahatius
Ahatius

Reputation: 4875

Appropriate way to output SQL Result Array in JSON

I'm outputting my DB result like this:

foreach($result as $row) {
    echo json_encode($row);
}

This outputs the following:

{"nummer":"600","name":"Location Name 600"}{"nummer":"698","name":"Location Name 698"}{"nummer":"1110","name":"Location Name 1110"}

Is this the right way? Do I have to seperate the result rows from each other?

Edit: If I get a callback id from jquery, what way would I format this into jsonp? To be more specific, I'd like to put an value containing the total amount of results, and then all results found, in jsonp (jquery sends me the id for a callback, which I'll have to put into the output).

Upvotes: 2

Views: 944

Answers (1)

C. Leung
C. Leung

Reputation: 6318

simply...

echo json_encode($result);

Upvotes: 3

Related Questions