如何自动从表格中获取最新数据(如输入的那样)

How to fetch latest data(as it is entered) from database automatically to my web page using PHP, AJAX, JQuery?

I used the following code and it is working fine. but I need to fetch and display an updated data on my web page as soon as a new data is entered in my database.

<?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;
?>

In the js save the last id of the data fetched, then simply compare it. For example create a query in php that only fetches the id, and run it every 5 seconds if the id differs, than fetch the php code that you posted.