基本上,我正在尝试在我的网站上放置一个实时源,以便将所有新数据推送到我的数据库中的外部API,
以下是我用来抓取 PHP 文件的当前 HTML 代码,并将所有新内容添加到数据库
$min_id_result = $DB->select("SELECT `id` FROM `api_media` ORDER BY `id` DESC",true);
//Basically all this is doing is returning the latest ID in the database
var Id = "<?=$last_id_result['id'];?>";
function waitForPics() {
$.ajax({
type: "GET",
url: "/ajax.php?id="+Id,
async: true,
cache: false,
success: function(data){
var json = eval('('+ data +')');
var img = json['img'];
if(json['img'] != "") {
$('<li/>').html('<img src="'+img+'" />').appendTo('#container')
}
Id = json['id'];
setTimeout(waitForPics,1000);
},
});
}
$(document).ready(function() {
waitForPics();
});
这是我用来处理数据库的 ajax.php 文件
$min_id_result = $DB->select("SELECT * FROM `api_media` ORDER BY `id` DESC",true);
$next_min_id = $min_id_result['id'];
$last_id = $_GET['id'];
while($next_min_id <= $last_id) {
usleep(10000);
clearstatcache();
}
$qry_result = $DB->select("SELECT * FROM `api_media` ORDER BY `id` DESC");
$response = array();
foreach($qry_result as $image) {
$response['img'] = $image['image'];
$response['id'] = $image['id'];
}
echo json_encode($response);
我遇到的问题是它没有从请求中返回任何数据给我,看起来对我来说都是正确的,但显然第二个开发人员眼睛看起来更好
答案 0 :(得分:0)
这应该有效:
while($next_min_id <= $last_id) {
usleep(10000);
clearstatcache();
$min_id_result = $DB->select("SELECT * FROM `api_media` ORDER BY `id` DESC",true);
$next_min_id = $min_id_result['id'];
}
在while
内等待特定参数更改,但您需要给它更改的机会。
变量本身不会改变, 你需要刷新它们。