954,604 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

PHP View Friends From Database

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.


<?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"";	
}
?>


Thanks in advance

doctorphp
Junior Poster in Training
67 posts since Apr 2010
Reputation Points: 10
Solved Threads: 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 ");
storm123
Newbie Poster
11 posts since May 2010
Reputation Points: 10
Solved Threads: 2
 

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

doctorphp
Junior Poster in Training
67 posts since Apr 2010
Reputation Points: 10
Solved Threads: 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 sayingWarning: 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

doctorphp
Junior Poster in Training
67 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 
$numrows = mysql_num_rows($sql);
if($numrows==0)
{
die ("You currently do not have any friends on photoshare");
}
farhan386
Light Poster
44 posts since Jul 2008
Reputation Points: 10
Solved Threads: 7
 
$numrows = mysql_num_rows($sql);
if($numrows==0)
{
die ("You currently do not have any friends on photoshare");
}

Thank you very much!

doctorphp
Junior Poster in Training
67 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: