I have 3 Tables: Messages, Users, and Friends.
Messages: msg_id, uid_fk, message, alert, created, uploads, owner
Users: uid, first_name, last_name
Friends: friend_one, friend_one
When a user becomes friends with another user, I record their uid’s in the following manner: friend_one:172 friend_two:58 | friend_one: 58 friend_two: 172
My code below works perfectly and goes through to select all of the user’s message updates. My problem is that I need to select all of the messages of not only the user himself/herself but also all of his or her friends messages too.
The following below as mentioned above selects only the user’s status updates and I have been struggling for hours to select not only the user’s status updates but also his or her friends with it.
$query = $db->prepare("SELECT M.msg_id, M.uid_fk, M.message, M.alert,
M.created, M.uploads , M.owner , U.uid , U.first_name , U.last_name FROM
messages M, users U WHERE M.uid_fk=U.uid and M.uid_fk= :uid_fk limit 10");
$query->execute(array(':uid_fk' => $uid));
($uid is session id of logged-in user)
Also my code for selecting friends just in case:
$friends_sql = $db->prepare("SELECT a.first_name, a.uid, a.last_name FROM users
a, friends b WHERE a.uid = b.friend_two AND b.friend_one = :friend_one ORDER BY
b.friend_id DESC LIMIT 8");
$friends_sql->execute(array(':friend_one' => $uid));
The following should return the most recent 10 messages for a user and the user’s friends.