Reputation:
I've followed the instructions from question 7076525 with no luck. I've added my [] but for some reason it only puts them around every third row? I'm having trouble figuring out what syntax to put in my code to make this work.
// output data of each row
while($row = $result->fetch_assoc()) {
$rows[] = $row;
//Add the header...
header('Content-Type: application/json');
echo json_encode($rows);
}
} else {
echo "0 results";
}
Here is what my json data looks like currently:
[
{
"ticket_id": "66",
"number": "000005",
"user_id": "109",
"user_email_id": "0",
"status_id": "3",
"dept_id": "1",
"sla_id": "1",
"topic_id": "36",
"staff_id": "1",
"team_id": "0",
"email_id": "0",
"flags": "0",
"ip_address": "10.18.1.195",
"source": "Phone",
"isoverdue": "0",
"isanswered": "1",
"duedate": null,
"reopened": null,
"closed": "2015-04-20 07:16:46",
"lastmessage": "2015-04-20 07:16:46",
"lastresponse": "2015-04-20 07:16:46",
"created": "2015-04-20 07:16:46",
"updated": "2015-04-20 07:16:46"
}
][
{
"ticket_id": "66",
"number": "000005",
"user_id": "109",
"user_email_id": "0",
"status_id": "3",
"dept_id": "1",
"sla_id": "1",
"topic_id": "36",
"staff_id": "1",
"team_id": "0",
"email_id": "0",
"flags": "0",
"ip_address": "10.18.1.195",
"source": "Phone",
"isoverdue": "0",
"isanswered": "1",
"duedate": null,
"reopened": null,
"closed": "2015-04-20 07:16:46",
"lastmessage": "2015-04-20 07:16:46",
"lastresponse": "2015-04-20 07:16:46",
"created": "2015-04-20 07:16:46",
"updated": "2015-04-20 07:16:46"
},
{
"ticket_id": "67",
"number": "000006",
"user_id": "129",
"user_email_id": "0",
"status_id": "3",
"dept_id": "1",
"sla_id": "1",
"topic_id": "36",
"staff_id": "1",
"team_id": "0",
"email_id": "0",
"flags": "0",
"ip_address": "10.18.1.121",
"source": "Other",
"isoverdue": "0",
"isanswered": "1",
"duedate": null,
"reopened": "2015-04-20 07:25:42",
"closed": "2015-04-20 07:25:54",
"lastmessage": "2015-04-20 07:18:33",
"lastresponse": "2015-04-20 07:25:54",
"created": "2015-04-20 07:18:33",
"updated": "2015-04-20 07:25:54"
}
][
{
"ticket_id": "66",
"number": "000005",
"user_id": "109",
"user_email_id": "0",
"status_id": "3",
"dept_id": "1",
"sla_id": "1",
"topic_id": "36",
"staff_id": "1",
"team_id": "0",
"email_id": "0",
"flags": "0",
"ip_address": "10.18.1.195",
"source": "Phone",
"isoverdue": "0",
"isanswered": "1",
"duedate": null,
"reopened": null,
"closed": "2015-04-20 07:16:46",
"lastmessage": "2015-04-20 07:16:46",
"lastresponse": "2015-04-20 07:16:46",
"created": "2015-04-20 07:16:46",
"updated": "2015-04-20 07:16:46"
},
{
"ticket_id": "67",
"number": "000006",
"user_id": "129",
"user_email_id": "0",
"status_id": "3",
"dept_id": "1",
"sla_id": "1",
"topic_id": "36",
"staff_id": "1",
"team_id": "0",
"email_id": "0",
"flags": "0",
"ip_address": "10.18.1.121",
"source": "Other",
"isoverdue": "0",
"isanswered": "1",
"duedate": null,
"reopened": "2015-04-20 07:25:42",
"closed": "2015-04-20 07:25:54",
"lastmessage": "2015-04-20 07:18:33",
"lastresponse": "2015-04-20 07:25:54",
"created": "2015-04-20 07:18:33",
"updated": "2015-04-20 07:25:54"
},
Upvotes: 0
Views: 59
Reputation: 360662
You're doing your encoding INSIDE the loop. It should be done after the loop:
while(...) {
build array
}
echo json_encode($array);
As it stands now, you're outputting multiple separate json strings, e.g.
{"foo":"bar"}{"baz":"qux"}
which is syntactically illegal JSON. If you do the encoding AFTER the loop, you'd get
[{"foo":"bar"},{"baz":"qux"}]
which IS legal.
And on top of that, you'd be outputting your header()
on each loop iterating, causing a ton of "headers already sent" warning messages, which further adds to the corruption.
A json response must contain one single syntactically valid json string, and nothing else.
Upvotes: 3
Reputation: 3551
You have to put the JSON encoding outside your while loop:
while($row = $result->fetch_assoc()) {
$rows[] = $row;
}
//Add the header...
header('Content-Type: application/json');
echo json_encode($rows);
Otherwise, you are just concatenating multiple JSON arrays, which result in an invalid JSON overall.
Upvotes: 1