I want to append new data once if it was added on sql in last 20 second.
So I used below script in my wall.php page.
Now in my server.php file query I created as
$timestamp = date("Y-m-d H:i:s", time() - 20);
$results = mysqli_query($dbh,"SELECT * FROM comments_lite WHERE qazi_id='1012' AND `date` > '".$timestamp."' ORDER BY date DESC LIMIT 1") or die(mysqli_error($dbh));
I also tried which not work:
WHERE qazi_id='1012' AND `date` >= NOW() - INTERVAL 20 SECOND ORDER BY date DESC LIMIT 1
My sql date format is 2015-02-25 19:45:28
But my query get same data in case of I used $timestamp
one after one which above 5+ minute also.
If the post occurred time is 20:20:00 and my query's current time is 20:20:10, it will be display the post. but if my query's current time is 20:20:30 (more then 20 second from post occurred time) , it will not display anything.
Please give me a guideline to solved it " append new data once if added on sql in last 20 second ".
My full code here below:
js:
<script type="text/javascript" charset="utf-8">
function addmsg(type, msg){
$("#messages").append(
"<div class='msg "+ type +"'>"+ msg +"</div>"
);
}
function waitForMsg(){
$.ajax({
type: "GET",
url: "/server/server.php",
async: true,
cache: false,
timeout:50000,
success: function(data){
addmsg("new", data);
setTimeout(
waitForMsg,
1000
);
},
error: function(XMLHttpRequest, textStatus, errorThrown){
setTimeout(
waitForMsg,
15000);
}
});
};
$(document).ready(function(){
waitForMsg();
});
</script>
Server.php
include("../db.php");
global $dbh;
header('Content-Type: application/json; charset=utf-8');
while (true) {
//fetch data
$timestamp = date("Y-m-d H:i:s", time() - 20);
$results = mysqli_query($dbh,"SELECT * FROM comments_lite WHERE qazi_id='1012' AND `date` > '".$timestamp."' ORDER BY date DESC LIMIT 1") or die(mysqli_error($dbh));
$rows = mysqli_fetch_assoc($results);
$data = $rows['description'];
//has data
if (!empty($data)) {
echo json_encode($data);
flush();
exit(0);
}
sleep(5);
}