0

I am using the code below to get information from a database and make it into JSON (it may be wrong).

Unfortunately it won't load in my web browser, it just says it's loading but it doesn't finish. Please can you tell me what I am doing wrong.

$query = mysql_query("SELECT * FROM Posts ORDER BY date DESC") or die(mysql_error());
$array = array(); 
while ($row = mysql_fetch_assoc($query)) { 
$array[] = $row; 
$postID =     $row['id']; 
while ($ra = mysql_fetch_assoc(mysql_query("SELECT * FROM Comments WHERE postID = '$postID'"))) { 
$array['comments'] = $ra; 
} 
while ($rd = mysql_fetch_assoc(mysql_query("SELECT * FROM Likes WHERE postID = '$postID'"))) { 
$array['likes'] = $rd;
 } 
} 
echo json_encode($array);

1 Answer 1

3

You are executing mysql_query in the infinite loop: on each iteration you query the database, and fetch the first row. Change it to

$res = mysql_query("SELECT * FROM Comments WHERE postID = '$postID'");
if (!$res)
{
   // handle error
}
while ($ra = mysql_fetch_assoc($res)) 
{
 ....
}

And the same for your second query.

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.