0
因此,我构建了所有基本功能,但存在一个主要缺陷。我从最后插入的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);
}
好吧我已经做了更多的研究,因为这似乎没有太多。 显然在客户端搜索最大记录,然后将其与id>超过服务器端select中的记录进行比较。 解决方案:[link](http://stackoverflow.com/questions/3665227/whats-the-fastest-way-to-poll-a-mysql-table-for-new-rows) –