0

I am trying to print out some database entries into an associative array, but it's only printing the first entry.

This is my code

if (mysqli_num_rows($result) > 0) {
    $data_array = array();
    while($row = mysqli_fetch_assoc($result)) { 
        $data_array['name']=$row["name"];
        $data_array['title']=$row["title"];
        $date=date_create($row['published']);
        $edited_date=date_format($date,"l, F d, Y");
        $data_array['date']=$edited_date; 
    }
} else {
    echo "0 results";
} 
echo json_encode($data_array);

1 Answer 1

2

You're replacing the data each time. You need to add more depth to your array:

while($row = mysqli_fetch_assoc($result)) { 
    $data_array[]['name']=$row["name"];
    $data_array[]['title']=$row["title"];
    $date=date_create($row['published']);
    $edited_date=date_format($date,"l, F d, Y");
    $data_array[]['date']=$edited_date; 
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for saving my day, you're my hero :)
One day I'll also be someone's hero.

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.