0
$retrieve = mysql_query("SELECT * FROM `users`") or die(mysql_error());

$object -> userDetails = array();

while($retrieveArray = mysql_fetch_array($retrieve))
{
    $item -> userId =  $retrieveArray['id'];
    $item -> userEmail = $retrieveArray['email'];
    $item -> userLocation = $retrieveArray['location'];
    $item -> userFirstName = $retrieveArray['firstname'];
    $item -> userLastName = $retrieveArray['lastname'];
    $object ->userDetails[] = $item;
}   
$json = json_encode($object);
echo $json;

Is there something wrong with this code? My output only displays the first row of my database.

{"userDetails":[{"userId":"1","userEmail":"[email protected]","userLocation":"HomeAddress","userFirstName":"Allan","userLastName":"Knocks"}]}
6
  • @JugalThakkar - They aren't. My problem is that my database contains three rows but my output returns only one. The output is at the lower part of my question. Commented Aug 4, 2012 at 8:04
  • Stop using the old mysql_* functions, they are deprecated. Use PDO or mysqli instead. If you var_dump($item) inside the while loop, how many times does it print? Since you are not creating a new $item object in the loop, the later iterations just rewrite the fields of the existing object - you should do $item = new stdClass() or similar inside the loop to avoid this. Commented Aug 4, 2012 at 8:07
  • try array_push($object ->userDetails, $item); instead of the last line inside your for loop Commented Aug 4, 2012 at 8:07
  • mysql_fetch_assoc() did a different thing. It displayed 3 rows but with the same values. Commented Aug 4, 2012 at 8:09
  • array_push($object ->userDetails, $item); did the exact thing with what mysql_fetch_assoc() did. Displayed 3 rows with the same values from the first row. Commented Aug 4, 2012 at 8:11

3 Answers 3

1

Try this:

$retrieve = mysql_query("SELECT id, email, location, firstname, lastname FROM `users`") or die(mysql_error());
$userDetails= array();
while($row = mysql_fetch_assoc($retrieve)) {
  $userDetails[] = $row;
}   
$json = json_encode(array('userDetails' => $userDetails));
echo $json;
Sign up to request clarification or add additional context in comments.

2 Comments

This is great. Short but perfect. Thanks!
still curious, @xdazz, could you throw some light on the major downside of the previous approach? Why it failed?
1

Beware! Doing this in a loop will modify the same instance many times and you will end up with the same object added many times to your array:

while($retrieveArray = mysql_fetch_array($retrieve))
{
    $item -> userId =  $retrieveArray['id'];
    $item -> userEmail = $retrieveArray['email'];
    $item -> userLocation = $retrieveArray['location'];
    $item -> userFirstName = $retrieveArray['firstname'];
    $item -> userLastName = $retrieveArray['lastname'];
    //you are adding here the same object with changed properties.
    $object ->userDetails[] = $item;
}  

In the end $object->userDetails will contain n references to the same object with the last set properties.

Instead you should create a new instance inside the loop :

while($retrieveArray = mysql_fetch_array($retrieve))
{
    //new item (if there is a class Item)
    $item = new Item();
    // if $item is just a stdObject created on the fly then use 
    $item = new stdClass();
    $item -> userId =  $retrieveArray['id'];
    $item -> userEmail = $retrieveArray['email'];
    $item -> userLocation = $retrieveArray['location'];
    $item -> userFirstName = $retrieveArray['firstname'];
    $item -> userLastName = $retrieveArray['lastname'];
    //you are adding here another object
    $object ->userDetails[] = $item;
}

2 Comments

Lol..that was an example... You didn't post the entire code so I guessed your class name. What's it called?
No.. I'm sorry if you misunderstood.
0

You can try

 $sql = "SELECT * FROM users";
 $result = $con->query($sql);
 if ($result->num_rows > 0) {   

  $userDetails = array();
  while ($row = $result->fetch_assoc()) {     
       $userData[] = array(
        "id" => $row["id"],
        "name" => $row["name"],
        "email" => $row["email"],
     );
   }
} else {
   echo "0 results";
}

 $result1['regular'] = $userData;
 $json = json_encode($result1);
  echo $json;

Your json will look like bellow

{"regular":[{"id":"2","name":"enamul Haque","email":"[email protected]"},{"id":"55","name":"Rafiq","email":"[email protected]"}]}

Comments

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.