hi everyone!
i`m trying to display a friend of a friend from a friend table,but i dont know how to do it.
i have a table called friends,which has id,friendid and userid fields.
Now i want if a member loggin, to be displayed a list of friends of his friends which are not his friends.
i hope u u got me guys!i`m hopping to hear frm You.
below is what i tried to but it gave me infinite datas.

<?php
	   //Connect to the database server
	
    include"config.php";
    $user=$_SESSION['user'];
    $logged_id=$_SESSION['id'];
	//select all your friends
	$select=mysql_query("SELECT *FROM friends WHERE userid='$logged_id' ORDER BY RAND() LIMIT 2");
	$count_friends=mysql_num_rows($select);
	 if($count_friends>0){
	   while($r=mysql_fetch_array($select)){
	      $friendid=$r['friendid'];
		   //select friends of a friend except yourself and which ar not ur friends
		    $sqr=mysql_query("SELECT *FROM friends WHERE ((userid='$friendid') and (((userid !='$logged_id') or (friendid !='$friendid')) or (userid !='$friendid') or (friendid !='$logged_id')))");
			  while($raw=mysql_fetch_array($sqr)){
			      $friendoffriend=$raw['friendid'];
				  $_query = mysql_query("SELECT * FROM profile WHERE id = '$friendoffriend'");
                  $_row = mysql_fetch_array($_query);
                  $name=$_row['names'];
				  echo"$name fomt";
				  
				  
			  }
			 
	   
	}
	 }else{
	 
	 echo"You must add friends";
	   
	 } 
?>

What do you mean when you say it is giving you infinite results? It is not limiting your result set to 2 ?

wELL IT SEEMS LIKE AFTER I PUT THE
WHILE LOOP TWO TIMES ,IT IS MAKING MY SCRIPT TO LOOP MANY TIMES AND GIVE LOTS OF RESULTS.

CAN YOU SUGGEST ME A BETTER WAY OF DOING THIS PLEASE???

I'm a bit confused about your second query but I'll take your word for it that it is doing what it's supposed to. Set a limit on your second query just like you did on the first, it will limit your result set to just 2.

$sqr=mysql_query("SELECT *FROM friends WHERE ((userid='$friendid') and (((userid !='$logged_id') or (friendid !='$friendid')) or (userid !='$friendid') or (friendid !='$logged_id'))) ORDER BY RAND() LIMIT 2");

I'm not sure what criteria your query is looking for but it does look like a lot of it could be cleaned up an simplified by using a JOIN or a subquery.

sO U MEAN THE WHOLE CODE WILL BE LIKE THIS

include"config.php";
    $user=$_SESSION['user'];
    $logged_id=$_SESSION['id'];
$sqr=mysql_query("SELECT *FROM friends WHERE ((userid='$friendid') and (((userid !='$logged_id') or (friendid !='$friendid')) or (userid !='$friendid') or (friendid !='$logged_id'))) ORDER BY RAND() LIMIT 2");

AND HERE THE WHILE LOOP AND DISPLAY ???

Well u said u a bit confused of my code,thats true.!because i wrote that as my first idea And i`m asking You to suggest a how to do this.
HERE IS THE WHOLE TASK I`M SUPPOSED TO DO.
Lets say u login in my a web,and you have friends.
Your friends also have got friends which some of them are not Your friends.
so i want to Display those friends of your friends which are not Your friends.

Structure of Friends Table is
id,friendid and userid (userid is your id after u login,which i store it in sessions.)

I HOPE FROM HERE yOU CAN HELP ME,HOW TO CODE THIS ISSUE.

$sqr=mysql_query("SELECT friendid FROM friends WHERE userid IN (SELECT friendid FROM friends WHERE userid='$logged_id') AND friendid NOT IN (SELECT friendid FROM friends WHERE user_id='$logged_id') ORDER BY RAND() LIMIT 2") or die(mysql_error());

// Do While Loop here

You can try that and see what you get. It may have to be tweaked a little bit.

Thank You,i`m trying it and i`ll update you as soon as possible.

@CFROG,Thank You very..the code works well.
except one thing,i also see myself because i`m also his friend.
I guess it need a little fix to hide myself($logged_id) from the list.

But it is such wonderful, i real appreciate man.

Like I said, it might have to be tweaked a little. I'm sure you can figure that part out on your own.

Glad it worked for you.

Sure! i will try ma self to fix the remaining part.
Thank You again.

Don't over-complicate it, it's an extremely easy fix. Just think it through.

Let me know if you run into a problem.

well here is how i solved it,i didn`t use sql as i`m not Good with Them., SO I JUST CHECKED WIT PHP

$logged_id=$_SESSION['id'];
	$sqr=mysql_query("SELECT friendid FROM friends WHERE userid IN (SELECT friendid FROM friends WHERE userid='$logged_id') AND friendid NOT IN (SELECT friendid FROM friends WHERE userid='$logged_id') ORDER BY RAND() LIMIT 10") or die(mysql_error());
	$countfriends=mysql_num_rows($sqr);
if($countfriends>0){

While($row=mysql_fetch_array($sqr)){
	     $friendidof=$row['friendid'];
               //CHECK IF IS THE LOGGED  ID
		 if($friendidof!=$logged_id){ 

            DISPAY RESULT
 }

}
}else{

echo"I SUGGEST YOU SHOULD ADD FRIENDS";

}

I had something slightly different in mind but it looks like you worked it out just fine either way. I'm glad you got it working the way you want.