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
https://forums.phpfreaks.com/topic/135955-minimize-number-of-mysql-queries/
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.