基本上,我试图在我的网站上为一个将所有新数据推送到我的数据库的外部 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);
我遇到的问题是它没有从请求中返回任何数据给我,这对我来说似乎都是正确的,但显然第二个开发人员的眼睛有时会更好