bd96500e110b49cbb3cd949968f18be7.png

I have the following code and it works great, I just want to convert it to live so it updates every 10 seconds or so without a page refresh, I'm guessing I'll need to use AJAX or Jquery but I lack the knowledge on how to do so.

=====VIA <?php include("database.php"); ?>====

// Create connection

$con=mysqli_connect("ip/host","user","pass","db");

// Check connection

if (mysqli_connect_errno()) {

echo "Failed to connect to MySQL: " . mysqli_connect_error();

}

?>

====ON THE PAGE====

php

$result = mysqli_query($con, "SELECT * FROM sql347511.1 ORDER BY ID DESC LIMIT 1;");

while ($row = mysqli_fetch_array($result)) {

echo "

Temperature: ".$row['TEMP']."°C
";

echo "

Humidity: ".$row['HUMID']."%
";

echo "

Captured: ".date("g:i:s a F j, Y ", strtotime($row["TIME"]))."
";

}

mysqli_close($con); ?>

解决方案

Got it working, thanks for the help everyone.

Javascript

$(document).ready(function(){

loadstation();

});

function loadstation(){

$("#station_data").load("station.php");

setTimeout(loadstation, 2000);

}

station.php

include ("database.php");

$result = mysqli_query($con, "SELECT * FROM sql347511.1 ORDER BY ID DESC LIMIT 1;");

while ($row = mysqli_fetch_array($result))

{

echo "

" . $row['TEMP'] . "°C
";

echo "

" . $row['HUMID'] . "%
";

echo "

At " . date("g:i:s a F j, Y ", strtotime($row["TIME"])) . "
";

}

mysqli_close($con);

?>

Where to put the data

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐