Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: can't encode json with multiple rows [duplicate]

Tags:

json

php

I've spent a couple of hours looking through several the similar answers before posting my problem.

I'm retrieving data from a table in my database, and I want to encode it into a JSON. However, the output of json_encode() is only valid when the table has one single row. If there is more than one row, the test at http://jsonlint.com/ returns an error.

This is my query:

$result = mysql_query($query);

    $rows = array();

    //retrieve and print every record
    while($r = mysql_fetch_assoc($result)){
        $rows['data'] = $r;

        //echo result as json
        echo json_encode($rows);
    }

That gets me the following JSON:

{
"data": 
    {
        "entry_id":"2",
        "entry_type":"Information Relevant to the Subject",
        "entry":"This is my second entry."
    }
}


{
"data":{
        "entry_id":"1",
        "entry_type":"My Opinion About What Happened",
        "entry":"This is my first entry."
    }
 }

When I run the test at http://jsonlint.com/, it returns this error:

    Parse error on line 29:
    ..."No comment"    }}{    "data": {    
    ---------------------^
    Expecting 'EOF', '}', ',', ']'

However, if I only use this first half of the JSON...

{
"data": 
    {
        "entry_id":"2",
        "entry_type":"Information Relevant to the Subject",
        "entry":"This is my second entry."
    }
}

... or if I only test the second half...

{
    "data":{
        "entry_id":"1",
        "entry_type":"My Opinion About What Happened",
        "entry":"This is my first entry."
    }
 }

... the same test will return "Valid JSON".

What I want is to be able to output in one single [valid] JSON every row in the table.

Any suggestion will be very much appreciated.

like image 999
asraelarcangel Avatar asked Feb 19 '12 18:02

asraelarcangel


2 Answers

The problem is you're spitting out separate JSON for each row, as opposed to doing it all at once.

$result = mysql_query($query);

$rows = array();

//retrieve and print every record
while($r = mysql_fetch_assoc($result)){
    // $rows[] = $r; has the same effect, without the superfluous data attribute
    $rows[] = array('data' => $r);
}

// now all the rows have been fetched, it can be encoded
echo json_encode($rows);

The minor change I've made is to store each row of the database as a new value in the $rows array. This means that when it's done, your $rows array contains all of the rows from your query, and thus you can get the correct result once it's finished.

The problem with your solution is that you're echoing valid JSON for one row of the database, but json_encode() doesn't know about all the other rows, so you're getting a succession of individual JSON objects, as opposed to a single one containing an array.

like image 169
mrlee Avatar answered Oct 19 '22 14:10

mrlee


You need to change your PHP code into something like this:

$result = mysql_query($query);

$rows = array();

//retrieve every record and put it into an array that we can later turn into JSON
while($r = mysql_fetch_assoc($result)){
    $rows[]['data'] = $r;
}
//echo result as json
echo json_encode($rows);
like image 22
Daan Avatar answered Oct 19 '22 16:10

Daan