Hello,
Looking for a CLEAN way to load posts with last 3 comments for each post without using subqueries. Is it possible to load posts and comments with the same query? Can I first load the posts with 1 query, the comments with second query and then combine both results? If yes, can you show an example?
Any help will be appreciated. Thanks.
Below are my script which uses subqueries. I need to get rid of subqueries because there are 20 posts per page. It means 20 queries to load comments.
$result = mysql_query("SELECT posts.id, posts.text, posts.type, posts.poster, posts.comments, users.profile_picture
FROM posts left join users on posts.poster=users.id order by posts.id DESC limit $page, $step") or die('Error1');
while(list($pid, $text, $type, $poster, $comments, $profile_picture) = mysql_fetch_row($result)) {
//print post
if ($comments != '0' {
$result1 = mysql_query("SELECT comments.id,comments.poster, comments.text, users.profile_picture FROM comments LEFT JOIN users ON comments.poster=users.id where comments.parent='$pid' order by comments.id DESC limit 0,3") or die('Error2');
while(list($cid,$cposter, $ctext, $cprofile_picture) = mysql_fetch_row($result1)) {
//print comments
}
}