$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"}]}
mysql_*functions, they are deprecated. Use PDO or mysqli instead. If youvar_dump($item)inside the while loop, how many times does it print? Since you are not creating a new$itemobject 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.array_push($object ->userDetails, $item);instead of the last line inside your for loop