I have created a "People You May Know" script in php. This script shows me the friends of my friends, in a php friend system. I have a table called users (user_id, name, surname, email, profile) thats holds information about the users. Another table called friends(friend_id, user_one, user_two) that holds the id of users that are friends. My code is the following:
<?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
?>
Everything works fine. All I want is to select and display 3 people, that are friends of my friends (because I may have a list many people that are friends of my friends). I use the LIMIT in my sql query as you can see but it is not working. Any ideas what to do?
Try this code. 3 friend ids will be printed. I have not tried.
$i=1;
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 --------------
if ($i <= 3)
{
$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'];
echo $not_friend_id;
$i++;
}
}//end if
}
}//end while
Try this:
<?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' LIMIT 3");
while ($run_not_friends= mysql_fetch_assoc($not_friends)) {
$not_friend_id = $run_not_friends['user_id'];
echo $not_friend_id;
}
}//end if
}//end while
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.