0

I am having an issue with how I am converting my php array into a JSON object. No matter what I try, I either print everything out as multiple objects or it comes out as null.Wrapping it in pre tags, here is the closest that I got it:

My code:

$content = mysqli_query($dbcon, 
  "SELECT title, last_name AS lastname
   FROM revision, field_last_name
   WHERE vid = entity_id;"
);

echo "<pre>";
while($row = mysqli_fetch_array($content))
{
  print json_encode($row);
  print '<br/>';
}
echo "</pre>";

My output:

{"0":"John Apple","title":"John Apple","1":"Apple","lastname":"Apple"}
{"0":"Kumar Patel","title":"Kumar Patel","1":"Patel","lastname":"Patel"}
{"0":"Michaela Quinn","title":"Michaela Quinn","1":"Quinn","lastname":"Quinn"}
{"0":"Peyton Manning","title":"Peyton Manning, MD","1":"Manning","lastname":"Manning"}
{"0":"John Doe","title":"John Doe","1":"Doe","lastname":"Doe"}
{"0":"Jane Lee","title":"Jane Lee","1":"Lee","lastname":"Lee"}
{"0":"Dan McMan","title":"Dan McMan","1":"McMan","lastname":"McMan"}
{"0":"Yu Win","title":"Yu Win","1":"Win","lastname":"Win"}

My two questions are:

1) Why is there a "0":"John Apple" and a "1":"Apple" when all I want is "title":"John Apple" and "lastname":"Apple" in my object?

2) Why is everything displaying as multiple objects?

Thanks!

---EDIT---

$arr = array()

echo "<pre>";   

while($row = mysqli_fetch_assoc($content))
{     
  $arr[] = $row;
}

print $arr;
echo "</pre>";
2
  • 1
    Because you are calling it in a while loop, it will json_encode every row. Commented Jan 9, 2014 at 19:45
  • field_last_name is your table name? can you distinguish each column name prefix by table name like revision.title in your query and get all data in a single array and then json_encode it? Commented Jan 9, 2014 at 19:45

3 Answers 3

1

field_last_name is your table name? can you distinguish each column name prefix by table name like revision.title in your query and get all data in a single array and then json_encode it?

   $content = mysqli_query($dbcon, 
      "SELECT title, last_name AS lastname
       FROM revision, field_last_name
       WHERE vid = entity_id;"
    );
    $arr = array();

    echo "<pre>";
    while($row = mysqli_fetch_assoc($content))
    {
      $arr[] = $row;
    }
      print_r(json_encode($arr));


    echo "</pre>";
Sign up to request clarification or add additional context in comments.

6 Comments

I have looked into what you had said and adding the table name in front of the column name does not change my results. Using the while loop you have provided has just resulted in a blank white screen for me please see the edit I made above
can you print_r($row) in while loop? and let me know what you have got?
That seemed to have did it, but I do not understand what you changed to make it work? Could you please explain?
@scapegoat17: it was firstly done. we should use print_r or var_dump to print array for this reason you did not see your results. You printed array by print. if it worked then accept it :)
Haha, I will accept it, but I still don't understand how doing what you did answered the first question that I had. Could you elaborate more on that?
|
1

Change this:

while($row = mysqli_fetch_array($content))
{
  print json_encode($row);
  print '<br/>';
}

To this:

$row = mysqli_fetch_assoc($content);
json_encode($row);

1 Comment

This would only print out one row and stop, not all rows in a single array object.
0

...because you're printing out multiple objects. If you want a single object which is an array, you need to append the results of mysql_fetch_assoc (see other answer covering field names vs positions) to an array, then json_encode the array in one shot. Example:

$myarray = array();
while($row = mysqli_fetch_assoc($content))
{
  $myarray[] = $row;
  print '<br/>';
}
print json_encode($myarray);

2 Comments

changing the to mysql_fetch_assoc has resulting in just displaying a blank white screen for me. Please see the edit to my questions that I made above.
Any time you get a blank screen, check your error_log. A blank screen means the script died. Assuming your edit is verbatim, you're missing a semicolon.

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.