PHP JSON Array not returning all rows from my data

2019-08-31 08:37发布

$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":"EmailAddress@gmail.com","userLocation":"HomeAddress","userFirstName":"Allan","userLastName":"Knocks"}]}

标签: php arrays json
3条回答
淡お忘
2楼-- · 2019-08-31 09:31

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;
}
查看更多
萌系小妹纸
3楼-- · 2019-08-31 09:36

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;
查看更多
时光不老,我们不散
4楼-- · 2019-08-31 09:39

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":"ehaque95@gmail.com"},{"id":"55","name":"Rafiq","email":"era@gmail.com"}]}
查看更多
登录 后发表回答