How to display users who are online, in the user table there are fields id , login , last_visit ?

I tried it, but it does not work.

 SELECT * FROM `user` WHERE `last_visit` < (NOW() - INTERVAL 15 MINUTE) 
  • What and how do you spell it in last_visit ? There the data can get when the player logged out or after half an hour, after committing the last action. And the timezone for what is written there? Data can be written there in UTC, and you’ll see it in local time - BOPOH
  • In the last_visit I write the timestamp type CURRENT_TIMESTAMP value is passed to NOW () - ZOymyng
  • and when is it written? all this should be in the task so that you do not have to re-read all the comments (there may be a lot of them) - BOPOH
  • @ZOymyng will be better if you lay out your table and several records on sqlfiddle.com - Alex

1 answer 1

Now you get those who have not been online for more than 15 minutes. Change the comparison operator:

 `last_visit` > (NOW() - INTERVAL 15 MINUTE) 

Will work provided that last_visit is updated correctly. For example, on any user action to perform.

 UPDATE user SET last_visit = NOW() WHERE id = [id_пользователя] 
  • but to work you need to do the same? add the update code for this field itself to the value of the mysql function NOW (). - ZOymyng
  • but in more detail you can, where to insert it and how? - ZOymyng
  • I think you know better. Is this your code? Or do you make requests from someone else's database? If your code is written to you, if the user is active (I do not know how it is described in your code), do an update. - Denis Fedichkin
  • my, but in general there are no standard solutions for such cases? - ZOymyng
  • How do you imagine that? You have a code that implements the work of the system, the authorization system is described, you make an entry in the table after authorization, for each user action (press the button, do something else) you update the date and time, this means the user is still active. - Denis Fedichkin