seriously, this code could use a serious revamp, doing several queries like this is not a good idea at all, it severely decrease your website's perfoemance. if an user has, like, 300 friends things will become sloppy.
$sql = "
SELECT us.username, us.userid
FROM friends fr, username us
WHERE (fr.user_1=$id && status=2 && us.userid = fr.user_2)
OR (fr.user_2=$id && status=2 && us.userid = fr.user_1)
";
$result = mysql_query($sql);
$total_friends = mysql_num_rows($result);
$str = '';
while ($row = mysql_fetch_assoc($result)) {
$str .= '<a href=?op=viewprofile&id='. $row['userid'] .'>'. $row['username'] . '</a>, ';
}
echo trim($str, ', ');
I havent tested this code but If there are no errors it will severely speed up your page.
either way, here's a fool-proof solution, but I higly recomment you use the one above
$sql = "SELECT * FROM friends WHERE user_1=$id && status=2";
$result = mysql_query($sql);
$total_friends = mysql_num_rows($result);
$str = '';
while ($row = mysql_fetch_array($result)) {
$sql2 = "SELECT * FROM users WHERE userid = " . $row['user_2'];
$query2 = mysql_query($sql2);
$arr2 = mysql_fetch_array($query2);
$str .= '<a href=?op=viewprofile&id='. $arr2['userid'] .'>'. $arr2['username'] . '</a>, ';
}
$sql3 = "SELECT * FROM friends WHERE user_2=$id && status=2";
$result3 = mysql_query($sql3);
$str2 = '';
while ($row = mysql_fetch_array($result3)) {
$sql4 = "SELECT * FROM users WHERE userid = " . $row['user_1'];
$query4 = mysql_query($sql4);
$arr4 = mysql_fetch_array($query4);
$str2 .= '<a href=?op=viewprofile&id='. $arr4['userid'] .'>'. $arr4['username'] . '</a>, ';
}
echo trim($str, ', ');