Whats is the right way to check for new messages? I have some messages in my mails table (MySQL).

I would like to send request via ajax every 5 seconds and look for new messages.

when i load the page first time, I use query like this

SELECT * FROM mails WHERE user_id = 1 LIMIT 100;


I'm displaying the messages in frontend as html table.

If any new messages received, how to only fetch new messages via AJAX?

I'm talking about the same way how gmail inbox works. When someone send you a mail, new messages is prepended in your browser mail list. 

Thanks
SELECT * FROM mails
    WHERE user_id = 1
      AND sequence &gt; $left_off
    ORDER BY sequence
    LIMIT 100;


You need a column sequence that is monotonically increasing.  And AJAX needs to keep track of "where it left off".