0

I am using ajax to pull some data from a PHP script I created. The PHP script queries the database for user information and comment information. There will be a lot of comments and I am trying to figure out how to return 3 records (LIMIT 3) - this is what I have:

foreach($results->fetch_assoc() as $key => $db_value) {
    //$array[$key] = $db_value;
    print_r($results->fetch_assoc());
}

$results->fetch_assoc() returns a single record set. I was messing around and couldn't figure it out.

What I am trying to do is have 3 arrays returned to the HTML doc via json_encode so that I can read it and display it. Can anyone help?

1 Answer 1

2
$data = array();

while ($row = $results->fetch_assoc()) {
   $data[] = $row; // stuff new row onto end of array
}

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

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.