问题描述:

I have created a "People you may know" script in php. I have 2 tables, users(user_id, name, surname, email, profile) and table friends(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).

网友答案:

Shankar Damodaran is right use this

$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;
网友答案:

Make use of a counter variable here like this

$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); echo"$num_of_people";

You are printing it wrong, that's a string not the variable.

  • Try this way.

    echo $num_of_people;

相关阅读:
Top