0

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

3
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by doctorphp
0

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 ");
0

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

Edited by doctorphp: n/a

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

Thank you very much!

Edited by doctorphp: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.