gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 08:49:15 24 4
gpt4 key购买 nike

我在 php 中创建了一个“您可能认识的人”脚本。我有 2 个表,users(user_id, name, surname, email, profile) 和表friends(friends_id, user_one, user_two)。我使用以下脚本来选择 friend 的 friend :

<?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。知道如何计算这么多人(我 friend 的 friend 不是我的 friend )。

最佳答案

像这样使用计数器变量

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

关于php - 无法计算 php 脚本中的结果数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18762658/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com