Skip to content
Advertisement

Insert into MySQL table if no row added within 5 minutes

I have a table which contains a column consisting of timestamps. I want to check if there is a new row within 5 minutes period of time. If not, then insert a new row or update the last row status column.

I tried something like this:

JavaScript

or insert into … but no success so far. I’m using PHP in WordPress if that matters.

Advertisement

Answer

I hope this helps you. If you want to add new row, you need to use INSERT command with selection like this…

JavaScript

If you want to update status column, you need to use simple UPDATE

JavaScript

Btw, you just need to have a last_visit column, and if it’s less than now + 5 min, you already know that the user is offline. And you have to update this value on every authorized request with simple update like.

JavaScript

So your User class will be have a function getStatus() like this:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement