Jump to content

minimize number of mysql queries


TheTitans

Recommended Posts

I have a script that displays the users who are online at the moment on a website.

 

<?php
$online = mysql_query ("SELECT onlineusr FROM online ORDER BY onlineusr ASC");
while ($onlinerow = mysql_fetch_array($online)) {
$rank_query = mysql_query("SELECT rank FROM users WHERE usrname = '" . $onlinerow['onlineusr'] . "'");
$row2 = mysql_fetch_array($rank_query);
?>
        <a href="profile.php?user=<?php echo $onlinerow['onlineusr'] ?>"><span style="color:<?php echo $row2['rank'] ?>"><?php echo $online['onlineusr'] ?></a><br />
<?php
}
?>

 

Let's say there are 100 people online. Then there would be 1 query for each user. I think that's too many. What can I do to simplify the amount of queries executed? I've done some research and found out that JOIN and UNION can be used to bring data together. Perhaps I use one of those methods?

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.