I am newbie about long poll. I want to update my wall if any new data in database by long. So as a small field I just try this below script which display undefined
continuously in index.php page.
In I used this code in server.php on test
, it works well.
while (true) {
$random = rand(1, 10);
$data = ($random == 1) ? array('aaa' => time()) : array();
if (!empty($data)) {
echo json_encode($data);
flush();
exit(0);
}
sleep(5);
}
Here, my problem is occurred when I was going to implement my php query
. I have no idea about, how to do it in my case.
How to make a query in server.php file and echo it's data at long poll?
I read about many more article about long poll, but I cannot find any good example to solved in my case.
My jQuery in index.php
function fetch() {
$.ajax({
url: 'http://bdshowbiz.com/server/server.php',
async: true,
type: 'get',
dataType: 'json',
success: function(ret, textStatus, jqXHR) {
$('#response').append('<p>' + ret.aaa + '</p>');
fetch();
},
error: function(jqXHR, textStatus, errorThrown) {
setTimeout(fetch, 5000);
}
});
}
$(function(){
fetch();
});
Current server.php file after implement query(may be it's wrong way)
include("../db.php");
global $dbh;
header('Content-Type: application/json; charset=utf-8');
while (true) {
//fetch data
$results = mysqli_query($dbh,"SELECT * FROM comments_lite WHERE qazi_id='1012' ORDER BY date DESC LIMIT 1") or die(mysqli_error($dbh));
$rows = mysqli_fetch_assoc($results);
$data = $rows['description'];
if (!empty($data)) {
echo json_encode($data);
flush();
exit(0);
}
sleep(5);
}