I had some help in another thread about having an issue with pulling data from 2 tables in MySQL, it appears I needed inner joins.
My Tables:
USERS
--id (int)
--username (varchar)
USER_FOLLOW
-- id (int)
-- follower (int)
-- user (int)
-- subscribed (current_timestamp)
The Query, $following =
SELECT ufollower.id AS follower_id,
ufollower.username AS follower_name,
ufollowed.id AS user_id,
ufollowed.username AS user_name
FROM
/* JOIN twice against users, once to get the follower and once to get the followed */
user_follow
/* users aliased as ufollower to get the follower details */
JOIN users ufollower ON ufollower.id = user_follow.follower
/* users aliased as ufollowed to get the followed details */
JOIN users ufollowed ON ufollowed.id = user_follow.user
WHERE
user_follow.user = $p_id
p_id is the profile ID of the person i’m looking at.
I need to show the usernames of who I’m following and who I’ve followed. The code I currently have for some reason is showing me 3 times instead of the 3 people I’m following:
while($apple = mysql_fetch_array($following)){
echo '<a href="'.$apple['user_name'].'">'.htmlspecialchars($apple['user_name']).'</a> ';
}
Sorry for opening another thread on this, I’ve been staring at it for hours and I can’t get my head around it.
At a glance it appears that you are displaying the wrong user’s name, showing the followed instead of the follower. Change your PHP code to the following:
Note that your query actually is only returning users that are following you, it does not include users that you are following (which would be those with
user_follow.follower = $p_id). Of course, for those users, you actually do want to display theuser_namecolumn…