E:
/
home
/
kbcomunicacao1
/
Web
/
innterage
/
webChat
/
Upload File
HOME
<?php $loopStart = time(); $updateAt = 3; $loopSeconds = 10; $pdo = new PDO("mysql:host=localhost;dbname=caom", "root", ""); if(isset($_POST["timestamp"])) { $timestamp = $_POST["timestamp"]; } else { $current = $pdo->prepare("SELECT NOW() AS now"); $current->execute(); $row = $current->fetchObject(); $timestamp = $row->now; } $timestamp = date($timestamp, time() + $updateAt); $sql = $pdo->prepare("SELECT * FROM cw_chat WHERE timestamp > :timestamp ORDER BY id_chat"); $sql->bindParam(":timestamp", $timestamp, PDO::PARAM_STR); $newMessages = false; $cw_chat = array(); while(!$newMessages && (time() - $loopStart) < $loopSeconds) { $sql->execute(); while($row = $sql->fetch(PDO::FETCH_ASSOC)) { $cw_chat[] = $row; $newMessages = true; } sleep($updateAt); } $current = $pdo->prepare("SELECT NOW() AS now"); $current->execute(); $row = $current->fetchObject(); $timestamp = $row->now; $data = array("cw_chat" => $cw_chat, "timestamp" => $timestamp); echo json_encode($data); exit;