php和ajax中的通知

How do I sort out and get the user's new notifications?

NOTIFICATION TABLE enter image description here

I have here a code that will get all the users notification

    //PHP PART
    "SELECT *
     FROM notifications
     WHERE tagged_by = "12042905" and action = \"unread\"";

    //JAVASCRIPT PART
    function n() {
        $.ajax({
           url: "notifications.php",
           dataType: "json",
           success: function(responseJSON) {
             $("div").html(reponseJSON[0].notify_id)
           }
        })
    }

    setInterval("n()", 1000)

But it gives me all the notifications every second. Now my problem is I only want to get the newly added row in the notifications table how can I do that and how can I output it one at a time? should I use .append() or .insert() in jQuery?

NOTE: date_notify syntax is y-m-d h:i:s or "2012-05-06 17:11:18"

You could simply add a limit to your MySQL query.

"SELECT *
FROM notifications
WHERE tagged_by = "12042905" and action = \"unread\"
LIMIT 1";

Your problem is, you are not UPDATING value of 'action' from 'unread' to 'read' after sending notification.
So the solution is simple: after each pull(select query), you should update all the rows which were just selected.

Also for column 'action', you should use 0 and 1 values instead of unread and read to optimize your database.

  Your problem is, you have a timer that keep on calling your function n() every 1sec at a time
  so the output will repeat until timer keep on running. , so to avoid that problem?

  1. The "Div" handler that will hold your ouput will empty first!
     ex.
        $("div").empty();
  2. try to use append()

     ex. $("div").append(reponseJSON[0].notify_id);

  Hope this will help! tnx