Kadage Posted October 1, 2011 Share Posted October 1, 2011 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 Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 1, 2011 Share Posted October 1, 2011 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); Quote Link to comment Share on other sites More sharing options...
Kadage Posted October 1, 2011 Author Share Posted October 1, 2011 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 1, 2011 Share Posted October 1, 2011 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}'"; Quote Link to comment Share on other sites More sharing options...
Kadage Posted October 2, 2011 Author Share Posted October 2, 2011 It works now!! Thanks heaps mjdamato! You help on this matter is much appreciated! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.