Ajax / php获取最新的数据通知

时间:2014-07-13 13:07:01

标签: javascript php ajax mysqli polling

我在获取最新数据时遇到了一些麻烦。我想要的是找出数据库中是否有新数据并将记录数量打印回我的页面。我有ajax工作,它有最后一个notify_id,它发送到PHP。我只需要让php选择任何新数据,然后将其发回并打印出来。

我还没有尝试投票,但是当我有这个工作并且更好地理解它时会调查它。

<?  $call="SELECT * 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_id=mysqli_fetch_array($chant))
            {
                ?>
            <script type="text/javascript">
setInterval(function(){


  var notification_id="<?php echo $notification_id['notification_id'] ;?>"

$.ajax({
type: "GET",
url: "viewajax.php?notification_id="+notification_id,   
dataType:"json",
cache: false,
success: function(data){
$(".mess"+notification_id).prepend("<span class'mess' id='mes'>"+response['notification_id']+"</span>");

}
});
},20000);

</script>
<? }?>

echo'';

PHP viewajax.php - 将其更改为JSON - 但在响应中获取{“notification_id”:null}

   <?php
session_start();
 include"database.php";

if(isset($_GET['notification_id']))
{
$id=$_GET['notification_id'];
$user1_id=$_SESSION['id'];
$json = array();
$com=mysqli_query($mysqli,"select notification_id from notifications where notification_id>'$id' AND notification_targetuser='$user1_id' AND notification_status=1");

$resultArr = mysqli_fetch_array($com);
$json['notification_id'] = $resultArr['notification_id'];
mysqli_free_result($com);


echo json_encode($json);
 }?>

1 个答案:

答案 0 :(得分:1)

如果您想要新数据,为什么不只是notification_id > '$id'代替notification_id='$id'

我假设notification_id是一个整数索引列,$id是收到的最后一个ID前端。