shan2batman Posted November 12, 2015 Share Posted November 12, 2015 Guys im trying to join a comments table to posts table using the following query in a method. public function feedView($session,$friend,$updateid) { $sql2=" select u.update_body,u.author,u.time,u.title,u.account_name,u.update_id," . "c.comment_body, c.os_id,c.author_c,c.time_c,c.comment_id,c.type_c " . "from updates u join comment_update c " . "on c.os_id=:statusid WHERE u.account_name = :session or u.account_name=:friend and (u.type = 'a' or 'c') order by u.time asc,c.time_c desc"; $stmth= $this->_db->prepare($sql2); $stmth->bindValue(":session",$_SESSION['uname']); $stmth->bindValue(":friend",$friend); $stmth->bindValue(":statusid",$updateid); $stmth->execute(); return $stmth->fetchAll(PDO::FETCH_ASSOC); } it prints the posts fine but the problem is with comments content where it seems to print the comments in the same posts twice. don't know where the bug is coming from.here is how it is displaying feeds:- Quote Link to comment Share on other sites More sharing options...
Solution Psycho Posted November 12, 2015 Solution Share Posted November 12, 2015 The problem is not the query. The problem is likely the logic in displaying the output. I expect you have a loop to process all the records in the result set. You need to have logic so the first record in the result set display the post content and the first comment (if it exists). Each subsequent record in the result set should only display the comment. But, there is a problem with the query. It uses a normal JOIN. So, if there are no records in the comments table, the post won't be returned either. This needs to use a LEFT JOIN so all relevant posts will be returned - even if they do not have comments. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.