Jump to content

On/offline Query


stelthius

Recommended Posts

Hey guys just a quick one to see if any one has any tips on cleaning this up,

 

$query = mysql_query("SELECT u.username FROM ".TBL_USERS." u INNER JOIN ".TBL_ACTIVE_USERS." a ON a.username=u.username WHERE a.timestamp < (NOW() - INTERVAL 5 MINUTE) AND u.username= '" . $value[$x]['username'] . "'")or die(mysql_error());

if (mysql_num_rows($query) > 0)	// Check to see if person is Online
$status = "<img src=\"images/online.gif\"><br />";
else
$status = "<img src=\"images/offline.gif\"><br />";

 

Basicly just use it to tell me when a user(s) is on/offline but i feel its not as clean as it could be any tips appretiated.

Link to comment
https://forums.phpfreaks.com/topic/159490-onoffline-query/
Share on other sites

Based on this - $value[$x]['username'] you have the posted code inside of a loop. It is extremely inefficient to execute a query in a loop to retrieve a set of same type values. You should form a query that retrieves all the matching rows in a single query and then loop through the result set.

 

Look at the mysql IN() function to get all the u.username's that are in your $value array in one query - http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_in

Link to comment
https://forums.phpfreaks.com/topic/159490-onoffline-query/#findComment-841298
Share on other sites

Ok thanks for the advice PFM, one last question ive looked on google for some tutorials or guides on combining two queries but i cant find anything that looks correct.. has anyone got any advice or tutorials they could link me please so i can learn how to do this properly and efficiantly

Link to comment
https://forums.phpfreaks.com/topic/159490-onoffline-query/#findComment-841318
Share on other sites

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.