I am trying to create a Real-time chart, with the data pulling from my own server. I am using the setInterval function to refresh the data from the server, but I can't refresh the value. Can anyone help me with this? Thank you Here is my source code:
main.php
<?php
require 'data_refresh.php';
?>
<script type="text/javascript">
function pull(){
value1 = <?php echo $value1; ?>;
reading_time = <?php echo $reading_time; ?>;
},
pull();
var chartT = new Highcharts.Chart({
//Some code for creating the chart
});
setInterval(pull(),2000);
Here is the data_refresh.php file
<?php
$servername = "";
$dbname = "";
$username = "";
$password = "";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, value1,reading_time FROM sensordata order by reading_time desc limit 40";
$result = $conn->query($sql);
while ($data = $result->fetch_assoc()){
$sensordata[] = $data;
}
$readings_time = array_column($sensordata, 'reading_time');
$value1 = json_encode(array_reverse(array_column($sensordata, 'value1')), JSON_NUMERIC_CHECK);
$reading_time = json_encode(array_reverse($readings_time), JSON_NUMERIC_CHECK);
$result->free();
$conn->close();
?>