【发布时间】:2013-09-10 09:16:27
【问题描述】:
我在 php.ini 中创建了一个“你可能认识的人”脚本。这个脚本向我展示了我朋友的朋友,在一个 php 朋友系统中。我有一个名为 users (user_id, name, surname, email, profile) 的表,其中包含有关用户的信息。另一个名为 friends(friend_id, user_one, user_two) 的表保存了好友用户的 ID。我的代码如下:
<?php
//----- get friends of friends -------
$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 ");
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)) {
$i=1;
if ($i <= 3)
{
$not_friend_id = $run_not_friends['user_id'];
echo $not_friend_id;
$i++;
}
}
}//end if
}//end while
?>
一切正常。我想要的只是选择并显示 3 个人,他们是我朋友的朋友(因为我可能有一个列表很多人是我朋友的朋友)。如您所见,我在我的 sql 查询中使用了 LIMIT,但它不起作用。有什么想法吗?
【问题讨论】:
-
尝试将 LIMIT 3 更改为 LIMIT 0,3
-
$not_friends = mysql_query("SELECT
user_id,name,surname,email,profileFROMusersWHERE (user_id='$friend_friend_id' AND @ 987654329@!='$session_user_id') 限制 3;"); - 它只会返回一行。这里的限制是没有用的 -
好的..知道在哪里设置限制吗?
-
@user2491321> 建议:不要多次使用这样的查询,因为那样会对服务器造成压力。
-
你是对的......你还有什么想法吗?