hoponhiggo Posted June 21, 2011 Share Posted June 21, 2011 Morning chaps. Can anybody help me with the code i would need to display the friends of a session user from this table? frienshipID-------friendname--------username 1 -------------username1--------username2 Im still a beginner with SQL and PHP so any advice would be gratefully received. I have been trying to use something along the lines of: $getfriends = mysql_query("SELECT friendname, username FROM friends where friendname OR username = '{$_SESSION['MM_Username']}'") Quote Link to comment https://forums.phpfreaks.com/topic/239958-display-mutual-friends/ Share on other sites More sharing options...
AMcHarg Posted June 21, 2011 Share Posted June 21, 2011 I'm not sure I entirely understand the question but your SQL is incorrect. You need: $getfriends = mysql_query("SELECT friendname, username FROM friends where friendname = '{$_SESSION['MM_Username']}' OR username = '{$_SESSION['MM_Username']}'") Quote Link to comment https://forums.phpfreaks.com/topic/239958-display-mutual-friends/#findComment-1232731 Share on other sites More sharing options...
hoponhiggo Posted June 21, 2011 Author Share Posted June 21, 2011 Ok So this code works fine to display all records where the session user is mentioned in the records $getfriends = mysql_query("SELECT friendname, username FROM friends where friendname = '{$_SESSION['MM_Username']}' OR username = '{$_SESSION['MM_Username']}'") ; //loops there name out while ($user = mysql_fetch_array($getfriends)) { echo "<a href='members.php?user=$user[username]'>$user[username]</a><br> $img <br> "; But i need it to only display the freind of the session user, not the session users themselves. Does this make sense or is more clarification needed? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/239958-display-mutual-friends/#findComment-1232742 Share on other sites More sharing options...
AMcHarg Posted June 21, 2011 Share Posted June 21, 2011 Try using an IF statement... //loops their name out while ($user = mysql_fetch_array($getfriends)) { if ($user[username] == $_SESSION['MM_Username']}) { echo "<a href='members.php?user=$user[friendname]'>$user[friendname]</a><br>$img <br>"; } else { echo "<a href='members.php?user=$user[username]'>$user[username]</a><br>$img <br>"; } } Hope it helps. Quote Link to comment https://forums.phpfreaks.com/topic/239958-display-mutual-friends/#findComment-1232759 Share on other sites More sharing options...
hoponhiggo Posted June 21, 2011 Author Share Posted June 21, 2011 Thanks. I have tried this code an their seems to be a syntax error. Any idea where? dreamweaver is telling me it is on the 'if' and first 'echo' lines? The full code is: (but the error is only on the last few lines) <?php echo "<center>"; if(isset($_GET['user'])) { //if there trying to view a profile //gets the user name and makes it safe $username = $_GET[user]; //querys the db to find the username $getuser = mysql_query("SELECT * FROM `users` WHERE `username` = '$username'"); //checks see if the username exists in the db $usernum = mysql_num_rows($getuser); //if it don't exist if($usernum == 0) { //don't exist echo ("User Not Found"); } //if it does exist then show there profile else{ $user = mysql_fetch_array($getuser); //to display image from source $dir = "prof_pics"; $sql = "SELECT prof_pic FROM users WHERE username = '$username'"; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) == 0) die("Username not found in database."); $row = mysql_fetch_array($res); $pic="$dir/".$row['prof_pic']; $img="<img src=\"$pic\" width=\"88\" height=\"88\" align=\"center\"><br>"; echo " <b>$user[username]'s Profile</b><br><br> $img <br> Email: $user[email]<br> <a href='friendrequest.php?user=$user[username]'>Add as Friend</a> "; } }else{ //gets all the members from the database $getfriends = mysql_query("SELECT friendname, username FROM friends where friendname = '{$_SESSION['MM_Username']}' OR username = '{$_SESSION['MM_Username']}'") ; while ($user = mysql_fetch_array($getfriends)) { if ($user[username] == $_SESSION['MM_Username']}) { echo "<a href='members.php?user=$user[friendname]'>$user[friendname]</a><br>$img <br>"; } else { echo "<a href='members.php?user=$user[username]'>$user[username]</a><br>$img <br>"; } } echo "<center>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/239958-display-mutual-friends/#findComment-1232835 Share on other sites More sharing options...
php_begins Posted June 21, 2011 Share Posted June 21, 2011 Your curly brace } seem to be at the wrong place .. Quote Link to comment https://forums.phpfreaks.com/topic/239958-display-mutual-friends/#findComment-1232839 Share on other sites More sharing options...
AMcHarg Posted June 21, 2011 Share Posted June 21, 2011 Ah that's what I get for copy+paste too much. Remove the curly bracket. Quote Link to comment https://forums.phpfreaks.com/topic/239958-display-mutual-friends/#findComment-1232855 Share on other sites More sharing options...
hoponhiggo Posted June 21, 2011 Author Share Posted June 21, 2011 Thanks guys. I had to remove a { from place and then place a } in another. Would never have got there without your help tho. Thanks Hopon Quote Link to comment https://forums.phpfreaks.com/topic/239958-display-mutual-friends/#findComment-1232942 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.