3

我在 php.ini 中创建了一个“你可能认识的人”脚本。我有 2 个表,users(user_id, name, surname, email, profile)和表Friends(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。知道如何计算这么多人(不是我朋友的朋友的朋友)。

4

3 回答 3

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
?>
于 2013-09-12T11:21:46.237 回答
0

$num_of_people=mysql_num_rows($not_friends); echo"$num_of_people";

你打印错了,那是一个字符串而不是变量。

  • 试试这个方法。

    回声 $num_of_people;

于 2013-09-12T11:21:14.793 回答
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;
于 2013-09-12T11:30:53.020 回答