来自PHP的数据库的实时更新值

时间:2016-08-11 20:15:26

标签: php html mysql

我正在尝试在PHP中执行一个脚本,当它们在mySQL数据库中更改时更新两个值。我的问题是它不起作用。由于我不熟悉php,我认为它需要在输出echo之前运行整个脚本。因为我在我的while循环中没有得到任何回声输出。

我想在新的值到达时删除旧值,这就是ob_end_clean()的原因;命令。

我希望能有一些帮助来解决这个问题!我不想刷新整个页面,只是在数据库中更改时输出新值。

提前谢谢!

    <?php
    //DB INFO
     $db = mysqli_connect('test.domain.mysql','test_domain_test','testtest','test_domain_test')
     or die('Error connecting to MySQL server.');
    ?>

    <html>
     <head>
     </head>
     <body>
     <h1>IFRAME</h1>

    <?php
    ob_start();
    $tempSensor1 = 0;
    $tempSensor2 = 0;

    //Get values
    $query = "SELECT * FROM `values`";
    mysqli_query($db, $query) or die('Error querying database.');


    $result = mysqli_query($db, $query);
    $row = mysqli_fetch_array($result);

    //Writes to the page the latest value in the table.
    echo $row['sensor1'] . $row['sensor2'] . '<br />';

   // Saves the values temporarily.
    $tempSensor1 = $row['sensor1'];
    $tempSensor2 = $row['sensor2'];

   //Infinity loop to update the values if the changes.    
    while(1){
    $query = "SELECT * FROM `values`";
    mysqli_query($db, $query) or die('Error querying database.');

    $result = mysqli_query($db, $query);
    $row = mysqli_fetch_array($result);

    //If new values in DB, write these out.   
    if($row['sensor1'] != $tempSensor1 or $row['sensor2'] != $tempSensor2) {
        ob_end_clean();
        echo $row['sensor1'] . $row['sensor2'] . '<br />';
    }
    $tempSensor1 = $row['sensor1'];
    $tempSensor2 = $row['sensor2'];
    sleep(5);
    }

    //Close connection to DB
    mysqli_close($db);

    ?>

    </body>
    </html>

0 个答案:

没有答案