所以我构建了所有的基本功能,但有一个主要缺陷。我从插入的最后一个id进行搜索,这很好。。但错误是当我使用PHP时。我搜索MAX id,它显然会发回最后一条记录。。但是,如果用户执行了三个向您发送通知的操作,那么其他两个操作不会通过调用显示,只有在页面刷新时才会显示。
那么,当每次调用都是从它在前端搜索的最后一个id进行时,我必须更改什么才能从数据库中获取所有通知呢?我想可能是PHP页面中的while循环,而不是最大id。但后端如何从前端的最后一个最大id中选择所有新数据?
所以这就是我所拥有的。
<?
$user1_id=mysqli_real_escape_string($mysqli,$_SESSION['id']);
$call="select MAX(notification_id) AS notification_id ,notification_status,notification_targetuser,notification_triggeredby,notification_throughurl from notifications WHERE notification_targetuser='$user1_id' AND notification_status=1 ORDER BY notification_id DESC LIMIT 1";
$chant=mysqli_query($mysqli,$call) or die(mysqli_error($mysqli));
while($notification=mysqli_fetch_array($chant)){
?>
<script type="text/javascript">
var notification_id="<?php echo $notification['notification_id']?>";
var notification_targetuser="<?php echo $notification['notification_targetuser']?>";
var notification_triggeredby="<?php echo $notification['notification_triggeredby']?>";
function loadIt() {
$.ajax({
type: "GET",
url: "viewajax.php?notification_id="+notification_id+"¬ification_targetuser="+notification_targetuser+"¬ification_triggeredby="+notification_triggeredby,
dataType:"json",
success: function(data){
if(data.notification_id>notification_id){
$("#notif_ui"+notification_id).prepend('<div class="notif_text"><div id="notif_actual_text-" class="notif_actual_text"><img border="1" src="userimages/cropped'+data['notification_triggeredby']+'.jpg" onerror=this.src="userimages/no_profile_img.jpeg" width="40" height="40" ><br /><a href="'+data['notification_throughurl']+'">'+data['notification_content']+' </a><br />'+data['notification_time']+'<br/></div></div></div><hr/>');
i = parseInt($("#mes").text()); $("#mes").text((i+data.num));
if(!data.notification_id.length) {
//no results...
return;
}
notification_id = data.notification_id;
}
}
});
}
setInterval(loadIt, 10000);
</script>
PHP
$json = array();
$com=mysqli_query($mysqli,"select MAX(notification_id) AS notification_id,notification_content,notification_time,notification_triggeredby,notification_throughurl from notifications where notification_id > '$id' AND notification_targetuser=".$_GET['notification_targetuser']." AND notification_triggeredby=".$_GET['notification_triggeredby']." AND notification_status=1");
$num = mysqli_num_rows($com);
if($num>0){
$json['num'] = $num;
}else{
$json['num'] = 0;
}
$resultArr = mysqli_fetch_array($com);
$json['notification_id'] = $resultArr['notification_id'];
$json['notification_content'] = $resultArr['notification_content'];
$json['notification_throughurl'] = $resultArr['notification_throughurl'];
$json['notification_triggeredby'] = $resultArr['notification_triggeredby'];
$json['notification_time'] = $resultArr['notification_time'];
mysqli_free_result($com);
echo json_encode($json);
}
mysqli_free_result($com);
,防止返回多个结果集,因此将其从查询中删除。