-1
I would like you to only perform longpolling if the if($row["usuario"]=="123"), but I don’t know how to do this because it’s for a chat and any request that someone send to the comic all the chats are affected is affecting other chats.
If I put one if($row["usuario"]=="123"){ executar o longpolling somente nesta linha do bd } already solve, follow my code:
<?php
$timestart = time();
$pdo = new PDO('mysql:host=localhost;dbname=carrinho', 'root', '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 chat WHERE timestamp > '$timestamp'");
$sql->execute();
$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;
?>
I think it’s something here: while($Row = $sql->fetchAll(PDO::FETCH_ASSOC)){ $notifications = $Row; $newData = true; }
– Anderson