1
I am creating a system and I want it to bring all the results already recorded in the database, but it only returns the recorded data from the moment I refresh the page, for example if I insert 10 records in the bank, the 10 will appear at the time I am on the page, but when I update, everyone disappears!
In the bank I have a table called longpolling
and the columns id
, notificacao
(varchar(200)
), and timestamp
(timestamp).
Long Polling PHP script:
<?php
$timestart = time();
$pdo = new PDO('mysql:host=localhost;dbname=longpolling', 'root', '');
if(isset($_POST['timestamp'])){
$timestamp = $_POST['timestamp'];
}else{
$pega_time = $pdo->prepare("SELECT NOW() as now");
$pega_time->execute();
$row = $pega_time->fetchObject();
$timestamp = $row->now;
}
$sql = $pdo->prepare("SELECT * FROM notificacoes WHERE timestamp > '$timestamp'");
$newData = false;
$notificacoes = array();
while(!$newData && (time()-$timestart)<20){
$sql->execute();
while($row = $sql->fetchAll(PDO::FETCH_ASSOC)){
$notificacoes = $row;
$newData = true;
}
usleep(500000);
}
$pega_time = $pdo->prepare("SELECT NOW() as now");
$pega_time->execute();
$row = $pega_time->fetchObject();
$timestamp = $row->now;
$data = array('notificacoes' => $notificacoes, 'timestamp' => $timestamp);
echo json_encode($data);
exit;
Script Jquery:
<script type="text/javascript">
$(function(){
pegaNotificacoes();
});
function pegaNotificacoes(timestamp){
var data = {};
if(typeof timestamp != 'undefined') {
data.timestamp = timestamp;
}
$.post('longpolling.php', data, function(res){
for(i in res.notificacoes){
$('#resultados').append(res.notificacoes[i].notificacao+'<br>')
}
pegaNotificacoes(res.timestamp);
}, 'json');
}
What CSS and HTML have to do with the problem?
– Guilherme Nascimento
echo json_encode($data);
returns only the last query result? Or is it another type of information? You wantSELECT * FROM notificacoes WHERE timestamp > '$timestamp'
return all results by Ajax?– Guilherme Nascimento
When using
$notificacoes = $row;
you are replacing the previous notifications, but still, at least one notification should be returned...– Oeslei
@Guilhermenascimento exactly, I want you to bring all the results contained in the table.
– David Damasceno
@Oeslei it only returns from the moment I include a new record in the bank, if I include 10 at the time I am on the page it will return the 10, but if I update, all disappear!
– David Damasceno
Your comment has left things a little fuzzy. In the question you said that the results are only returned when refreshing the page and in the comment you said that they only return via Polling.
– Oeslei
Truth @Oeslei, I added at the end of the question what really happens, so it is via Polling that the records are disappearing from the page.
– David Damasceno
@Daviddamasceno Your problem is not in the Polling script... Maybe you are considering the
$timestamp = time();
in your script that displays notifications when accessing the page and how notifications are already older than thetime
current, they will not be displayed. Include your other script as well so we can analyze better.– Oeslei
@Oeslei am using only these two files in my studies, would have some solution to the problem?
– David Damasceno
One solution is to add a script to get all pending notifications before to start long Polling.
– Oeslei
Okay, thanks, I’ll try to implement.
– David Damasceno