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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.