slj90 Posted November 13, 2012 Share Posted November 13, 2012 I have a table 'friends' which has username and friend columns so if a user is friends with another user it is saved in this table. I also have another table with posts users have made. I am trying to make it only show posts from the users friends. However it is only running the query for the first 'friend' in the table. What's wrong? <?php $query = "SELECT * FROM friends WHERE username='$username'"; $result = mysql_query($query) or die (mysql_error()); while($row = mysql_fetch_array($result)) { $friend = $row['follow']; $query = "SELECT * FROM posts WHERE username='$friend'"; $result = mysql_query($query) or die (mysql_error()); while($row = mysql_fetch_array($result)) { $id=$row['id']; $post=$row['post']; echo $post; echo " posted by "; echo $friend; echo "<br><br><br>"; } } ?> Quote Link to comment Share on other sites More sharing options...
thara Posted November 13, 2012 Share Posted November 13, 2012 do you need to select the posts that one user have posted? Quote Link to comment Share on other sites More sharing options...
Barand Posted November 13, 2012 Share Posted November 13, 2012 use a single query with a JOIN SELECT * FROM friends f INNER JOIN posts p ON p.username=f.follow WHERE f.username='$username' Quote Link to comment Share on other sites More sharing options...
slj90 Posted November 13, 2012 Author Share Posted November 13, 2012 use a single query with a JOIN SELECT * FROM friends f INNER JOIN posts p ON p.username=f.follow WHERE f.username='$username' I don't quite understand how to do this... I just read a tutorial on it and came up with this: $query = "SELECT posts.post, friends.username, ". "FROM friends, posts ". "WHERE posts.username = friends.friend"; I'm confused, where does it get the usernames friends from? Quote Link to comment Share on other sites More sharing options...
thara Posted November 13, 2012 Share Posted November 13, 2012 I think Barand has missed something in his post.. I think query should something similar to this.. SELECT * FROM friends AS f INNER JOIN posts AS p ON p.username = f.follow WHERE f.username = '$username' This is not suit for you.. explain your 2 tables with columns... Quote Link to comment Share on other sites More sharing options...
slj90 Posted November 13, 2012 Author Share Posted November 13, 2012 Thanks Thara Table one 'Posts' has the columns 'Username' and 'Post'. Table two 'Friends' has the columns 'Username' and 'Friend' (friends username). I want to display all the posts that all friends of the logged in users has made. Thanks Quote Link to comment Share on other sites More sharing options...
Barand Posted November 13, 2012 Share Posted November 13, 2012 Table one 'Posts' has the columns 'Username' and 'Post'. Table two 'Friends' has the columns 'Username' and 'Friend' (friends username). If those are your column names, where did this come from $friend = $row['follow']; Quote Link to comment Share on other sites More sharing options...
thara Posted November 13, 2012 Share Posted November 13, 2012 (edited) If those are your column names, where did this come from $friend = $row['follow']; yes what does mean $friend = $row['follow']; why you use 'username' column in both table? Edited November 13, 2012 by thara Quote Link to comment Share on other sites More sharing options...
Barand Posted November 13, 2012 Share Posted November 13, 2012 I think Barand has missed something in his post.. I think query should something similar to this.. SELECT * FROM friends AS f INNER JOIN posts AS p ON p.username = f.follow WHERE f.username = '$username' This is not suit for you.. explain your 2 tables with columns... @thara If you ever get around to reading the MySQL manual you will see the the AS is optional, denoted by [ .. ] For each table specified, you can optionally specify an alias.tbl_name [[AS] alias] Can you please explain why a JOIN is not suitable here? Quote Link to comment Share on other sites More sharing options...
slj90 Posted November 13, 2012 Author Share Posted November 13, 2012 yes what does mean $friend = $row['follow']; why you use 'username' column in both table? $row['follow'] should be $row['friend'] Username is used in the post table to save who posted it. Username is used in the friends table so when a user is friends with someone it creates a row with their Username and their friends username. Thanks Quote Link to comment Share on other sites More sharing options...
Barand Posted November 13, 2012 Share Posted November 13, 2012 In that case SELECT f.friend, p.post FROM friends f INNER JOIN posts p ON p.username=f.friend WHERE f.username='$username' 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.