I'm trying to build a friend system for my website. The table structure is set up something like this:
CREAT TABLE users (
id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL
) Engine=InnoDB;
CREATE TABLE friendship (
member1 INT UNSIGNED NOT NULL, -- user's ID
member2 INT UNSIGNED NOT NULL, -- friend's ID
FOREIGN KEY (member1) REFERENCES users (id),
FOREIGN KEY (member2) REFERENCES users (id),
UNIQE (member1, member2)
) Engine=InnoDB;
Sample friendship data:
INSERT INTO friendship
VALUES
(1, 2),
(1, 3),
(1, 5);
I am able to get all of a user's (id 1, in this example) friend's ids by using the following:
$query = mysql_query("SELECT * FROM friendship_table WHERE member1='1'");
while($row = mysql_fetch_assoc($query)) {
$memberid = $row['member2'];
}
What I want to do now is a to get the friends' names. The memberid (member2 column in the table) variable has the values of 2,3 and 5 in the example. I want to do an if/elseif type of statement to see what the memberid is. Kind of like this:
if($memberid == "2") {
echo "Bob is your friend";
} elseif($memberid == "3") {
echo "Joe is your friend";
} elseif($memberid == "3" && $memberid == "2") {
//This line is the problem, the variable has multiple values in it (2,3,5) so I think it doesn't understand that and just uses one value
echo "Bob and Joe are your friends!";
} else {
//...
}
So the problem is that when I want to check if that variable has two specific values assigned to it, it doesn't check through all of its values. How can I make it so that it checks all of its values (2,3,5)?