I've a twitter streaming data in mysql and it keep adding every 5-10 record/second. Now, I want to get every new records that have created time start when client open the page and continuously, until the page is close. The data is json that contain geo-location for marking a map and it must be inside a javascript variable, here is json format:
{max data: 30; data: [{"lat":-6.92015,"lon":107.67024,"value":0.1},...]}
I've been digging into AJAX, but not success. Here my current code:
get_query.php
<?php
require_once('./db_connect.php');
$dbcon=new db;
$query="SELECT geo_lat, geo_long FROM location WHEN created_at >= NOW()";
$result = mysqli_query($dbcon, $query);
$data= array();
while($row= mysqli_fetch_assoc($result)){
$data[] = array("lat"=>(float)$row["geo_lat"], "lon"=>(float)$row["geo_long"], "value"=>0.1);
}
echo json_encode($data);
?>
data.js
var data = new array();
$(function() {
setInterval(function() {
$.ajax({
type: "GET",
url: "/absolute/path/of/get_query.php",
dataType: "json",
data: $(this).serialize(),
success: function(response) {
data = response;
}
});
}, 1000);
});
My question: 1. How to run my php code continuously after user open the page? 2. How to get data from php continuously without remove previous data with ajax? 3. It possibly to get event when user close the page to empty a record in mysql?
Thanks