我在php中创建了一个“你可能知道的人”脚本。我有2个表,用户(user_id,姓名,姓名,电子邮件,个人资料)和表朋友(friends_id,user_one,user_two)。我使用以下脚本来选择我朋友的朋友:
<?php
$friends_of_friends = mysql_query(" SELECT u.*
FROM (SELECT DISTINCT user_one as user_id
FROM friends
WHERE user_two IN (SELECT user_one as user_id
FROM friends
WHERE user_two = '$session_user_id'
UNION DISTINCT
SELECT user_two
FROM friends
WHERE user_one = '$session_user_id'
)
UNION DISTINCT
SELECT DISTINCT user_two
FROM friends
WHERE user_one IN (SELECT user_one as user_id
FROM friends
WHERE user_two = '$session_user_id'
UNION DISTINCT
SELECT user_two
FROM friends
WHERE user_one = '$session_user_id'
)
) f
JOIN users u
ON u.user_id = f.user_id ORDER BY `surname` ASC ");
while ($run_friends= mysql_fetch_assoc($friends_of_friends)) {
$friend_friend_id = $run_friends['user_id'];
// friends of my friends that are not my friends
$check_friend_query = mysql_query(" SELECT friends_id from friends WHERE (user_one='$session_user_id' AND user_two='$friend_friend_id') OR (user_one='$friend_friend_id' AND user_two='$session_user_id') ");
if(mysql_num_rows($check_friend_query) != 1){
$not_friends = mysql_query("SELECT `user_id`, `name`, `surname`, `email`, `profile` FROM `users` WHERE (`user_id`='$friend_friend_id' and `user_id`!='$session_user_id') ");
while ($run_not_friends= mysql_fetch_assoc($not_friends)) {
$not_friend_id = $run_not_friends['user_id'];
}
}//end if
}//end while
?>
我的代码成功运作。唯一的问题是,在我使用这个脚本获得所有想要的人之后,我无法计算他们的数量。我用过:
$num_of_people=mysql_num_rows($not_friends);
echo"$num_of_people";
我总是得到1.任何想法我怎么能算这个数量的人(朋友的朋友不是我的朋友)。
答案 0 :(得分:1)
在此处使用 counter variable
$i=0; // I have added here
while ($run_friends= mysql_fetch_assoc($friends_of_friends)) {
$friend_friend_id = $run_friends['user_id'];
$i++;// Increment the var
}//end while
echo $i;//Total number of users
?>
答案 1 :(得分:0)
$ num_of_people = mysql_num_rows($ not_friends); 回声 “$ num_of_people”;
你打错了,那是一个字符串而不是变量。
试试这种方式。
echo $ num_of_people;
答案 2 :(得分:0)
Shankar Damodaran正确地使用了这个
$i=0;
while ($run_friends= mysql_fetch_assoc($friends_of_friends)) {
$friend_friend_id = $run_friends['user_id'];
// friends of my friends that are not my friends
$check_friend_query = mysql_query(" SELECT friends_id from friends WHERE (user_one='$session_user_id' AND user_two='$friend_friend_id') OR (user_one='$friend_friend_id' AND user_two='$session_user_id') ");
if(mysql_num_rows($check_friend_query) != 1){
$not_friends = mysql_query("SELECT `user_id`, `name`, `surname`, `email`, `profile` FROM `users` WHERE (`user_id`='$friend_friend_id' and `user_id`!='$session_user_id') ");
if(mysql_num_rows($not_friends)) i++;
}//end if
}//end while
echo $i;