$query120 = "(SELECT username FROM friendship WHERE frenusername='{$username2}') UNION ALL(SELECT frenusername FROM friendship WHERE username='{$username2}')";
$result120 = mysql_query($query120,$connection) or die (mysql_error());
confirm_query($result120);
while($userinfo120 = mysql_fetch_array($result120)){
$frenusername2= $userinfo120['username'];
echo $frenusername2; // this will display both username and frenusername.
}
//if u want to separate 2 sets of datas (username and frenusername), u can modify the abit the code above to something like this :
$query120 = "(SELECT frenusername, NULL as username FROM friendship WHERE username='{$username2}' UNION ALL SELECT NULL, username FROM friendship WHERE frenusername='{$username2}'";
$result120 = mysql_query($query120,$connection) or die (mysql_error());
confirm_query($result120);
while($userinfo120 = mysql_fetch_array($result120)){
$frenusername2= $userinfo120['username'];
$frenusername1= $userinfo120['frenusername'];
echo $frenusername2;
echo $frenusername1;
}
//Anyway, UNION is not necessarily the way to go when you want to get two separate data sets, because the purpose of UNION is to combine 2 sets of datas.
//if you want to replace UNION with OR, it is not easy because you need to select two fields with OR condition, look at my table records below :
username | frenusername
---------------------------------
zac1987 | qq
zac1987 | bb
uu | zac1987
oo | zac1987
As you can see, both fields username and frienusername has "zac1987". For example, if $username2 = "zac1987", "SELECT frenusername, username FROM friendship WHERE frenusername='{$username2}' ";, it will show "zac1987" for the output because you SELECT frenusername, so how to hide zac1987? U can write something like this :
$query120 = "SELECT frenusername, username FROM friendship WHERE username='{$username2}' OR frenusername='{$username2}' ";
$result = mysql_query($query120) or trigger_error(mysql_error());
if(mysql_num_rows($result) > 0) {
while($row = mysql_fetch_assoc($result)) {
if($row['frenusername'] == $username2) {
$friends[] = $row['username'];
}
elseif($row['username'] == $username2) {
$friends[] = $row['frenusername'];
}
}
}
echo $username2 . ' has ' . count($friends) . ' friends. They are ' . implode(', ',$friends);
Posted by Zac1987 on
05 July, 2011
0 comments