I have coded up an example of exactly what the MySQL query needs to do, I had tried to combine the two queries but I didn’t have any success with doing so. Basically, what this does is selects all users that $session is not friends with already.
I don’t know how much I need to explain as far as my table structure goes, as you can see what I need to do using my query below, but basically my friends table only has one row for each friendship. (a friendship is only valid if the state is 1 and the CASE statement is necessary to get the friends ID from the friendship, since my table does only have one row for a friendship.
$getUsers = mysql_query("SELECT id FROM users WHERE id!=$session");
$getFriends = mysql_query("SELECT CASE WHEN userID=$session THEN userID2 ELSE userID END AS friendID
FROM friends
WHERE (userID=$session OR userID2=$session)
AND state='1'");
$usersArray = array();
$friendsArray = array();
while($usersC = mysql_fetch_array($getUsers))
{
$usersID = $usersC['id'];
array_push($usersArray, $usersID);
}
while($usersF = mysql_fetch_array($getFriends))
{
$friendID = $usersF['friendID'];
array_push($friendsArray, $friendID);
}
print_r(array_merge(array_diff($usersArray, $friendsArray), array_diff($friendsArray, $usersArray)));
You want an anti-join, which you can effect through an outer join and a filter for records where the joined table is
NULL:See it on sqlfiddle.