Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
$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"}]}
share|improve this question
    
all these are not null? –  Jugal Thakkar Aug 4 '12 at 8:02
    
@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. –  JetPro Aug 4 '12 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. –  DCoder Aug 4 '12 at 8:07
    
try array_push($object ->userDetails, $item); instead of the last line inside your for loop –  ladiesMan217 Aug 4 '12 at 8:07
    
mysql_fetch_assoc() did a different thing. It displayed 3 rows but with the same values. –  JetPro Aug 4 '12 at 8:09
show 2 more comments

2 Answers

up vote 1 down vote accepted

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;
share|improve this answer
    
This is great. Short but perfect. Thanks! –  JetPro Aug 4 '12 at 8:14
    
still curious, @xdazz, could you throw some light on the major downside of the previous approach? Why it failed? –  Jugal Thakkar Aug 4 '12 at 8:17
    
thanks for this. –  djrconcepts Oct 21 '13 at 19:01
add comment

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;
}
share|improve this answer
    
Class 'Item' not found. Your code gives me this error. –  JetPro Aug 4 '12 at 9:59
    
Lol..that was an example... You didn't post the entire code so I guessed your class name. What's it called? –  Samson Aug 4 '12 at 10:20
    
I've updated my post. Hope it's clear enough. –  Samson Aug 4 '12 at 10:43
    
Making fun of newbies? Not so cool. BTW thanks. –  JetPro Aug 4 '12 at 11:34
    
No.. I'm sorry if you misunderstood. –  Samson Aug 4 '12 at 11:53
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.