1

I am working on a web app using php. I am trying to pull from a database and fill an array with the information then encode it to json.

Here is what I have so far:

$result = mysqli_query($db, $query);
$row = mysqli_fetch_array($result);

$frats = array();
while ($row = mysqli_fetch_array($result)) {
    $frat->name = $row['name'];
    $frat->description = $row['description'];
    $frat->chapter = $row['chapter'];
    $frat->members = $row['members'];
    $frat->cover_image = $row['cover_image'];
    $frat->profile_image = $row['profile_image'];
    $frat->calendar_image = $row['calendar_image'];
    $frat->preview_image = $row['preview_image'];
    $frat->address = $row['address'];
    $frats[] = $frat;
}

echo json_encode($frats);

What I get in return is in the correct json format I want with the correct keys and everything, but every item in the json is the same. All of them are the last one. I believe this is because I might just be referencing the values, but I am not sure how to actually copy them in php.

Any suggestions?

2
  • 3
    You need to crest a new frat inside the loop Commented Oct 30, 2017 at 21:41
  • Where is $frat exactly being initialized and setup? Do you have that code? Commented Oct 30, 2017 at 21:43

3 Answers 3

3

you need to create a new instance of $frat inside the loop. Otherwise you are just updating the original $frat each time the loop runs and adding a reference to it in the array.

Assuming its not a static class you could use:

$frats = array();
while ($row = mysqli_fetch_array($result)) {
    $frat = new frat();
    $frat->name = $row['name'];
    $frat->description = $row['description'];
    $frat->chapter = $row['chapter'];
    $frat->members = $row['members'];
    $frat->cover_image = $row['cover_image'];
    $frat->profile_image = $row['profile_image'];
    $frat->calendar_image = $row['calendar_image'];
    $frat->preview_image = $row['preview_image'];
    $frat->address = $row['address'];
    $frats[] = $frat;
}

echo json_encode($frats);

Even better would be to modify the constructor or create a method to consume the data. If you used the class's constructor then you could do something like this:

$frats = array();
while ($row = mysqli_fetch_array($result)) {
    $frats[] &= new frat($row['name'], $row['description'], $row['chapter']);
}

echo json_encode($frats);
Sign up to request clarification or add additional context in comments.

Comments

1

In the first line inside your while loop, you simply need the following line:

$frat = new \stdClass();

2 Comments

(could be disastrous if $frat is not a stdClass object to begin with ... but the question is unclear where $frat even came from)
@Randall yep, there's no indication as to if $frat was anything before, who knows
0

You need to properly declare your variables $frats (array) and $frat (object):

$frats = array();
while ($row = mysqli_fetch_array($result)) {
  $frat = (object) array();
  $frat->name = $row['name'];
  $frat->description = $row['description'];
  $frat->chapter = $row['chapter'];
  $frat->members = $row['members'];
  $frat->cover_image = $row['cover_image'];
  $frat->profile_image = $row['profile_image'];
  $frat->calendar_image = $row['calendar_image'];
  $frat->preview_image = $row['preview_image'];
  $frat->address = $row['address'];
  array_push($frats, $frat); // For backward compatiblity
}

echo json_encode($frats);

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.