tags:

views:

26

answers:

3

when a user will log in his profile then he will c all of his friends in his home page,I have 2 tables,no1:-table name fsb_profile containing profile_id,profile_name,etc table no2:- fsb_friendlist containing friendlist_memberid,friendlist_friendid.now i want to show the friend details in home page,

my code is:-

$id=$_SESSION["_ID"];
$query = "select * from fsb_profile " .
    "where fsb_profile.profile_id=(" .
    "select fsb_friendlist.friendlist_friendid " .
    "from fsb_friendlist " .
    "where friendlist_memberid=" . $id . ")";

if( $sql = mysql_query($query) ) {
    while ($t = mysql_fetch_assoc($sql)) {
        echo "hai";
        echo $t["profile_name"];
    }
} else {
    echo "Something went horribly wrong.\n";
}
**error:-**Something went horribly wrong.
A: 

Change your code like this:

if( $sql = mysql_query($query) ) {
    while ($t = mysql_fetch_assoc($sql)) {
        echo "hai";
        echo $t["profile_name"];
    }
} else {
   die(mysql_error());
}

To see what went horribly wrong so that we could provide you with better possible solution.

Also make sure that you have put session_start() on top of your script.

Sarfraz
i have checked the session_start() its there,now the error becameSubquery returns more than 1 row
mriganka3
@mriganka3: You do not seem to have tried my code to see the error.
Sarfraz
A: 

You need to change your query to:

SELECT * FROM fsb_profile
    WHERE fsb_profile.profile_id IN (
        SELECT fsb_friendlist.friendlist_friendid
        FROM fsb_friendlist
        WHERE friendlist_memberid = $id
        );

i.e. replace fsb_profile.profile_id=( with fsb_profile.profile_id IN (

Edit:

I didn't pay attention to the actual query, I only tried to correct the error you are getting. The correct query should be something like:

SELECT * FROM fsb_profile
    WHERE fsb_profile.profile_id IN (
        SELECT friendlist_memberid
        FROM fsb_friendlist
        WHERE friendlist_friendid = $id
        );
Anax
it is showing the profile_name of the $id,but i want to show the name of the friends
mriganka3
its showing the $id name 3 times
mriganka3
A: 

You can use following query

SELECT * FROM fsb_profile WHERE fsb_profile.profile_id IN((SELECT GROUP_CONCAT() FROM fsb_friendlist WHERE friendlist_memberid=$id ))

jimy