建议朋友不是用户的朋友

时间:2014-02-06 09:09:53

标签: php mysql

我想为用户推荐朋友。我需要验证朋友的建议是否已经是用户的朋友 我有两个名为User,friendship

的表

我尝试了这段代码,但我无法继续,因为我没有更多的想法

$sql=“SELECT user.fullname,user.userID FROM users WHERE user. userID NOT 
     IN( SELECT * FROM friendship WHERE userID = '$userID') “;  

我正在使用php和mysql

2 个答案:

答案 0 :(得分:1)

尝试使用friendship.userID

$sql = "SELECT user.fullname,user.userID FROM users WHERE user. userID NOT 
 IN( SELECT friendship.userID FROM friendship WHERE userID = '$userID')";

您需要查看已经是用户朋友的UserIds

答案 1 :(得分:0)

这可能会帮助你..

function addFriend() {

global $userid, $friendid;

$check = mysql_query("SELECT * FROM friends WHERE userid = $userid AND friendid =     $friendid");

if (mysql_num_rows($check) == 1) {
   exit("Youre already friend with this user");
} else {

$sql = "INSERT INTO friends (userid, friendid) VALUES ($userid, $friendid)";
mysql_query($sql);
if (mysql_affected_rows()) {
  echo "Success";
else 
  echo "Failure";
}
}