dongou3158 2019-02-19 10:00
浏览 53

使用php运行对mysql的常量查询而不刷新HTML页面

Im looking at outputting mysql query results into a table on a php page where the 1st column of the table is the result and the 2nd column is populated with a Javascript timer

however my goal is to have it so that when data comes into the mysql database it display on the table and then when it stops being picked up by the query remove it from the table

the issue i face however is im not sure how to do this without resetting the javascript timer everytime the query is run,

I have gone through a couple of questions that have mentioned to use ajax and this would be fine but im not sure what i would put the HTML side

EDIT: the below is now my second attempt code using ajax but i cant get the table to display it errors as unexpected token < line 27

my current page load Code to call the data is as followed:

PHP

<?php
header('Content-type: application/json');
$servername = "localhost";
$username = "user";
$password = "password";
$dbname = "test";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 
#   header('Content-Type: applicaton/json');

$sql = "SELECT 
* 
FROM
(SELECT
  beacon,location,
  COUNT(location) AS counter 
FROM `track`
WHERE `date` = CURDATE() and `time` > NOW() - interval 60 second
GROUP BY beacon) AS SubQueryTable
ORDER BY beacon + 0 ASC;";
$result = $conn->query($sql);


$result = mysqli_query($conn ,  $sql);
   $rows = array();
   while($r = mysqli_fetch_assoc($result)) {
  $rows[] = $r;
    }

   echo json_encode($rows);
    $conn->close();
?>   

HTML

    $.get('vendor/fetch.php', function(response) {
        console.log(response);
        var row;
        response.forEach(function(item, index) {
            console.log(item);

(unexpexted token here) 
<table id="table">
    <?php
    while($row = mysqli_fetch_array($result))
    {
    ?>
    <tr style="background-color: <?php echo $row['item.location'];?>">
    <td><?php echo $row['item.beacon'];?></td>
    <td> <span class='minutes'>00</span>:<span class='seconds'>00</span> 
    </td>
    </tr>

    <?php } 

    mysqli_close($con);  
    ?>
    </table>
        });
    });


        function updateTable() {
        //console.log('function called');
        $.get('vendor/fetch.php', function(response) {
            response.forEach(function(item, index) {
                console.log(item.beacon);

            });
        });

    var updateTableInterval = setInterval(updateTable, 100);
    };
</script>

 </head>

 <body>
<script>
var sec = 0;

function pad(val) {
return val > 9 ? val : "0" + val;
}
var timer = setInterval(function () {
$(".seconds").html(pad(++sec % 60));
$(".minutes").html(pad(parseInt(sec / 60, 10)));
}, 1000);
</script> 
</body>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
    • ¥15 关于arduino编程toCharArray()函数的使用
    • ¥100 vc++混合CEF采用CLR方式编译报错
    • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
    • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
    • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
    • ¥50 如何openEuler 22.03上安装配置drbd
    • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
    • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
    • ¥15 Windows11, backspace, enter, space键失灵