如何使用PHP,AJAX,JQuery从数据库自动获取最新数据(如输入的那样)到我的网页?
我使用了以下代码,它运行正常。但是,只要在我的数据库中输入新数据,我就需要在我的网页上获取并显示更新的数据。
<?php
$hostname = '127.0.0.1';
$username = 'xxxxxxxxxxx';
$password = 'xxxxxxxxxxx';
try {
$dbh = new PDO("mysql:host=$hostname;
dbname=sensor_measurements",
$username, $password);
/*** The SQL SELECT statement ***/
$sth = $dbh->prepare("
SELECT `dtg` AS date,
`temp` AS temperature,
`humid` As humidity,
`acous` As acoustic,
`accel_x` As accelx,
`accel_y` As accely,
`accel_z` As accelz
FROM `sensor_data`
ORDER BY date DESC
LIMIT 1
");
$sth->execute();
/* Fetch all of the remaining rows in the result set */
$result = $sth->fetchAll(PDO::FETCH_ASSOC);
/*** close the database connection ***/
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
$json_data = json_encode($result);
echo $json_data;
?>
答案 0 :(得分:0)
在js中保存所提取数据的最后一个id,然后简单地比较它。例如,在php中创建一个只获取id的查询,如果id不同,则每5秒运行一次,而不是获取你发布的php代码。