user3304007 user3304007 - 3 months ago 6
MySQL Question

mysql fetch records which have been added in the last 10 seconds?

I want to fetch records that have been added in the last 10 seconds. I have a "zaman" column which has the timestamp of the record that tells when it has been added.

I have tried this one, however, it doesnt work (no errors)

mysql_query("SELECT * FROM notifs where writer='".$member[nick]."' AND
zaman >= DATE_SUB(NOW(),INTERVAL 10 SECOND) ORDER BY id DESC limit 5")
or die(mysql_error());


What is the correct way to do it ?

Answer

When a new record is saved, record it's time saved. Then, when running the fetch query, get all values that are greater than or equal to current time - 10. assuming zaman is a epoche timestamp...

mysql_query("SELECT * FROM notifs where writer='".$member[nick]."' AND 
zaman >= UNIX_TIMESTAMP()-10 ORDER BY id DESC limit 5")