简体   繁体   English

无法计算php脚本中的结果数量

[英]cannot count the number of results in a php script

I have created a "People you may know" script in php. 我在php中创建了一个“你可能知道的人”脚本。 I have 2 tables, users(user_id, name, surname, email, profile) and table friends(friends_id, user_one, user_two) . 我有2个表, 用户(user_id,姓名,姓名,电子邮件,个人资料)和表朋友(friends_id,user_one,user_two) I use the following script in order to select people that are friends of my friends: 我使用以下脚本来选择我朋友的朋友:

<?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

?>

My code works succesfully. 我的代码成功运作。 The only problem is that after I get all the people that I want using this script, I cannot count their number. 唯一的问题是,在我使用这个脚本获得所有想要的人之后,我无法计算他们的数量。 I have used: 我用过:

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

I am always getting 1. Any idea how can I count this amount of people(friends of my friends that are not my friends). 我总是得到1.任何想法我怎么能算这个数量的人(朋友的朋友不是我的朋友)。

Make use of a counter variable here like this 像这样使用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
?>

$num_of_people=mysql_num_rows($not_friends); $ num_of_people = mysql_num_rows($ not_friends); echo"$num_of_people"; 回声 “$ num_of_people”;

You are printing it wrong, that's a string not the variable. 你打错了,那是一个字符串而不是变量。

  • Try this way. 试试这种方式。

    echo $num_of_people; echo $ num_of_people;

Shankar Damodaran is right use this 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM