Jump to content

PHP MySQL Friend's Online Script Help


Kadage

Recommended Posts

Hey everyone,

 

I'm currently working on a friends online script and i have a slight problem that i need help with.

 

Basically the code first searches "TBL_Friends" to see if you have any friends added. If it returns results it then turns your friends ID's into a variable. It then searches "TBL_Users_Online" to see if any body is logged based on the friend's ID it returned before. The first bit of the code works and it retrieves all the friends i got added. The second half is odd, if i have one or two friends added it will show that one is online. If i have more then three friends added it returns no results. I know my code is a bit sloppy and probably not the best way of writing it, im still learning PHP.

 

Anyways this is the code, any help is appreciated.

 

<?php
$FriendsOnline = mysql_query("SELECT Sender_ID FROM TBL_User_Friends WHERE Reciever_ID = $UserID");
while($fo=mysql_fetch_array($FriendsOnline))
{
     $FriendsOnlineID = $fo[sender_ID];

     $FriendsOnlineNumber = mysql_query("SELECT * FROM TBL_Users_Online WHERE User_ID = $FriendsOnlineID");
     $FriendsNumber = mysql_num_rows($FriendsOnlineNumber);

     echo $FriendsNumber;
}
?>

 

$SenderID = Friends ID

$Reciever_ID = User ID

$UserID = User ID

Link to comment
https://forums.phpfreaks.com/topic/248213-php-mysql-friends-online-script-help/
Share on other sites

You should NEVER run queries in loops. Learn to do JOINs.

 

I think your problem is that you are running a query for each friend and then checking the count (which I would assume is 1 or 9(. But, you should run one query for all the friends.

 

$query = "SELECT COUNT(uo.User_ID) as online_count
          FROM TBL_User_Friends AS uf
          JOIN TBL_Users_Online AS uo
            ON uf.Sender_ID = uo.User_ID
          WHERE uf.Reciever_ID = '{$UserID}'";
$result = mysql_query($query);
$friends_online = mysql_result($result, 0);

Thanks Heaps! I got the number of friends displaying now. I got two more  question though, i have had a look around and i cant really find any answers as to how to get info back out of this query. And secondly can i join another table and get info out of that one as well. E.g. We got our friends id, we got our online friends id's, now i need to get info out of "TBL_Users". More specifically i need to get the "Name" "Email" and "Display_Photo" out of "TBL_Users". Then i echo that for every friend online.

I assume you want the "Name" "Email" and "Display_Photo" for the friend and not the user.

 

based on these requirements I would NOT use COUNT() since you need the additional info. You can just use mysql_num_rows() to get the count and your results will have all the data.

 

Try this (may need to modify the filed names as appropriate)

$query = "SELECT u.Name, u.Email, u.Display_Photo
          FROM TBL_User_Friends AS uf
          LEFT JOIN TBL_Users_Online AS uo
            ON uf.Sender_ID = uo.User_ID
          JOIN TBL_Users AS u
            ON uo.User_ID = u.User_ID
          WHERE uf.Reciever_ID = '{$UserID}'";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.