jquery - Ajax 结果用作 mysql 查询约束
问题描述
我每 60 秒使用 ajax 显示一些数据库数据,如下所示:
$(document).ready(function(){
function getData(){
$.ajax({
type: 'GET',
url: 'data.php',
success: function(data){
$('#output').html(data);
});
}
getData();
setInterval(function () { getData(); }, 60000); // it will refresh your data every 1 sec
});
该data.php
文件有一个类似的查询:
$sql = "select * from orders where time > date_sub(now(), interval 1 minute) ORDER BY id DESC";
如何记录 ajax 检索到的最后一个间隔并将其传递到查询中,以便它只显示前一个 ajax 结果 + 60 秒之间的记录?
我担心如果某处有延迟,它可能会省略一些记录。
解决方案
我最终以 json 格式将数据从 php 传递到 ajax:
function getData() {
$.ajax({
type: 'GET',
url: 'data.php',
data: {lastID: lastID},
dataType: 'json',
success: function (data) {
lastID = data[0].id;
console.log(lastID);
$.each(data, function (i, item) {
var $tr = $('<tr class="page-break">').append(
$('<td>').text(item.id),
$('<td>').text(item.name),
$('<td>').text(item.details)
).appendTo('#output');
});
function isEmpty(el) {
return !$.trim(el.html());
}
if (!isEmpty($('#output'))) {
window.print();
}
}
});
}
在 php 中:
$lastID = $_GET['lastID'];
$sql = "select * from orders where id > ".$lastID." ORDER BY id DESC";
推荐阅读
- javascript - 如何通过主屏幕书签(嵌入式)检测应用程序正在被访问?
- android - 与硬件、arduino 等相关的问题
- android - HttpURLConnection 没有保存文件
- pandas - 导入熊猫时无法导入名称“nosetester”错误
- keras - 为什么 tf 中的 optimizer.get_gradients(...) 不起作用?
- python - Pyspark groupBy DataFrame 没有聚合或计数
- javascript - 在 iframe 中触发“点击”事件
- php - 来自具有相同 ID 的数据库结果的多维数组 PHP
- ios - 拥有一个带有多个 UICollection 视图部分的主要粘性标题?Xcode - 斯威夫特
- firebase - 云函数影响读/写配额