Correctly updating the list of users online

I display all users online :

$result = mysql_query("SELECT * FROM users ");
while ($row = mysql_fetch_array($result)){
 echo "<br>";
 echo $row['username'];

How to update this list now?

Answer 1, authority 100%

The Users table must have fields:
1. online – the field answers – the user is in the game or not (1 – in the game, 0 – not in the game)
2. location – link to the location where the user was last tagged.

Then the request to select all users in a certain location is
“SELECT * FROM Users WHERE online=1 and location=”.$location

Broadcast a function to a timer that once every 5, 10, 20 minutes (as you wish) sends an Ajax request to the script and displays all users of this loca.

If the user has left the game, then update its field in the Users online=0 table.
If the user has moved to another locale, we update the location field for this user, and immediately make a request for all users located in this locale

Something like that

Answer 2

Easiest way – use jquery + ajax + setInterval(setTimeOut)