TheTitans Posted December 7, 2008 Share Posted December 7, 2008 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 More sharing options...
corbin Posted December 7, 2008 Share Posted December 7, 2008 A join is what you're looking for. SELECT u.usrname, u.rank FROM onlineusr o JOIN users u ON u.usrname = o.onlineusr ORDER BY o.onlineusr ASC; It would be better to join on a numeric ID, btw. Link to comment https://forums.phpfreaks.com/topic/135955-minimize-number-of-mysql-queries/#findComment-708785 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.