如何附加一个id';s的详细信息只通过php或jquery提供一次


How to append one id's detail only once by php or jquery

我想从sql only once中追加新添加的数据。

所以,我使用了下面的脚本来显示sql中新添加的数据,但它会一个接一个地附加相同的数据。

这意味着,如果我在sql中添加test by thisuser,它会连续追加4或5次。

如何制作,一个id的描述只显示一次。

我的Jquery:

function addmsg(type, msg){
    $("#messages").append(
        "<div class='msg "+ type +"'>"+ msg.description +" by "+ msg.username +"</div>"
    );
}
function waitForMsg(){
    $.ajax({
        type: "GET",
        url: "server.php",
        async: true, 
        cache: false,
        timeout:15000, 
        success: function(data){ 
            addmsg("new", data);
            setTimeout(
                waitForMsg, 
                1000 
            );
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            setTimeout(
                waitForMsg, 
                15000); 
        }
    });
};
$(document).ready(function(){
    waitForMsg(); 
});

server.php

global $dbh;
header('Content-Type: application/json; charset=utf-8');
while (true) {
date_default_timezone_set('Asia/Dhaka');
$datetime = date('Y-m-d H:i:s', strtotime('-15 second'));
$results = mysqli_query($dbh,"SELECT * FROM comments WHERE qazi_id='$id' AND date >= '$datetime' ORDER BY date DESC LIMIT 1") or die(mysqli_error($dbh));
$rows =  mysqli_fetch_assoc($results);
$row[] = array_map('utf8_encode', $rows);
$data = array();
$data['id'] = $rows['id']; /* I want use this id to display once*/
$data['likes'] = $rows['likes'];
$data['username'] = $rows['username'];
$data['img'] = $rows['img'];
$data['description'] = $rows['description'];
$data['parent_id'] = $rows['parent_id'];
$data['date'] = $rows['date'];
//has data
if (!empty($data)) {
    echo json_encode($data);
    flush();
    exit(0);
}
sleep(5);
}

尝试删除async: true,并进行检查。还有,为什么在成功部分再次调用waitformsg函数?setTimeout( waitForMsg, 1000 );