Member Avatar for doctorphp

Hi everyone

I am trying to develop a friend system where you can view friends who you are friends with.
I have been able to get it to show one result but it wont show any other. Please can someone tell me what I am doing wrong.
Here is the code

<?php
require("check_session.php");

$id = $_SESSION['id'];
$username = $_SESSION['username'];

?>
Here is a list of your current friends.
<br />
<br />
<?php

$sql = mysql_query("SELECT * FROM friends WHERE user_id='$id' AND friend='1'");
while($fetch = mysql_fetch_assoc($sql))
{

$friend_id = $fetch['friend_id'];

}
?>
<?php

$usertable = mysql_query("SELECT * FROM users WHERE id='$friend_id'");

while($row = mysql_fetch_assoc($usertable))
{
$table_first = $row['firstname'];
$table_last = $row['lastname'];

echo $table_first; echo" "; echo $table_last; echo"<br /><br />";	
}
?>

Thanks in advance

Recommended Answers

All 5 Replies

Hi, you doing some wrong things.
- here you get from DB all user frend, but in loop you use a one variable which will overwrite it value, you have to use a array to put in all geted values... you have to do this :

$sql = mysql_query("SELECT * FROM friends WHERE user_id='$id' AND friend='1'");
while($fetch = mysql_fetch_assoc($sql)) {
   $friend_id[] = $fetch['friend_id'];
}

now in $friend_id[] you have all friend_ids.
Now you have to to do this :

$frends = '('.implode(', ', $friend_id).')';
$usertable = mysql_query("SELECT * FROM users WHERE id IN $frends ");
Member Avatar for doctorphp

Thank you so much storm 123. Your help is really appreciated!

Member Avatar for doctorphp

Hi, you doing some wrong things.
- here you get from DB all user frend, but in loop you use a one variable which will overwrite it value, you have to use a array to put in all geted values... you have to do this :

$sql = mysql_query("SELECT * FROM friends WHERE user_id='$id' AND friend='1'");
while($fetch = mysql_fetch_assoc($sql)) {
   $friend_id[] = $fetch['friend_id'];
}

now in $friend_id[] you have all friend_ids.
Now you have to to do this :

$frends = '('.implode(', ', $friend_id).')';
$usertable = mysql_query("SELECT * FROM users WHERE id IN $frends ");

Is there a way to echo (die) a message to the user if they have no friends on their account because when I use 'mysql_num_rows' I get a message saying

Warning: implode() [function.implode]: Invalid arguments passed in /home/myusername/public_html/friends.php on line 30

PHP Error Message

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/Red/public_html/friends.php on line 33

This is what I added

$numrows = $sql;
if($numrows==0)
{
die ("You currently do not have any friends on photoshare");
}
else

Do you know what I should do?

Thanks in advance

$numrows = mysql_num_rows($sql);
if($numrows==0)
{
die ("You currently do not have any friends on photoshare");
}
Member Avatar for doctorphp
$numrows = mysql_num_rows($sql);
if($numrows==0)
{
die ("You currently do not have any friends on photoshare");
}

Thank you very much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.