I had the following code
<?php
$getFriendQuery = "SELECT DISTINCT U.username FROM users as U INNER JOIN test_friends as F on U.Id = F.user_id WHERE F.friend_id = '{$userID}' && F.active=1";
$getFriendResult = mysql_query($getFriendQuery, $conn) or die (mysql_error());
$friendName = "";
while($getFriendFetch = mysql_fetch_assoc($getFriendResult)){
$friendName .= $getFriendFetch['username'] . ", ";
}
$getFriendQuery = "SELECT u.username FROM users u INNER JOIN test_friends f ON u.id = f.user_id WHERE (f.friend_id = '{$userID}' AND active=1) OR (f.user_id = '{$userID}' AND active=1)";
$getFriendResult = mysql_query($getFriendQuery, $conn) or die (mysql_error());
while($getFriendFetch = mysql_fetch_assoc($getFriendResult)){
$friendName .= $getFriendFetch['username'] . ", ";
}
if (!empty($friendName)){
echo "Your friends: " . $friendName ;
} else {
echo "You do not have any friends yet";
}
?>
My code need 2 queries to get the "friends list". Is there a way to execute 1 query and can retrieve all friends list?
More information:
I got 2 tables. "Users table" and "test_friends" table. Users table has:
id represents user id => data type int(11)
username => data type varchar(256)
Test_friends table has:
user_id represents user id => data type int(11)
friend_id => data type int(11)
active => tinyint