2013-01-15 87 views
0

Possible Duplicate:
Server Side PHP Long polling服务器端长轮询PHP

我有在得到我的PHP做的,我想它是什么真正的问题。我试着长时间轮询服务器并输出下面的所有信息。

任何人都可以看看,看看我做错了什么。我只是需要它输出正确的细节。在服务器端streamitem_idcontent输出number 4由于某种未知的原因。而在客户端,我得到了RESPONSE NULL。

<script> 
setInterval("streamrefresh();", 2000); 

streamrefresh = function(){ 
    var timestamp = "<? echo $streamitem_data['streamitem_timestamp']; ?>"; 

    var last_msg_id = "<? echo $streamitem_data['streamitem_id']; ?>"; 
     $.ajax({ type: "POST", 
       url: "include/streampostinsert.php", 
       data:{ "timestamp":timestamp, "last_msg_id":last_msg_id}, 
       dataType:"json", 
       cache:false, 
       success: function(response){ 
        $("#homestatusid").prepend("<div id='divider-"+response['streamitem_id']+"'><div class='userinfo'><a href='/profile.php?username="+response['username']+"'><img class='stream_profileimage' style='border:none;padding:0px;display:inline;' border=\"0\" src=\"imgs/cropped"+response['id']+".jpg\" onerror='this.src=\"img/no_profile_img.jpeg\"' width=\"40\" height=\"40\" ></a><div style='cursor:pointer;position:relative;top:0px;float:right;padding-right:5px;' onclick=\"delete_('"+response['streamitem_id']+"');\">X</div><a href='/profile.php?username="+response['username']+"'>"+response['first']+" "+ response['middle']+" "+response['last']+"</a><span class='subtleLink'> said</span><br/><a class='subtleLink' style='font-weight:normal;'>"+response['streamitem_timestamp']+"</a><hr><div id='status_text_holder'>"+newmsg+"</div><div style='height:20px;' class='post_contextoptions'><div id='streamcomment'><a style='cursor:pointer;' id='commenttoggle_"+response['streamitem_id']+"' onclick=\"toggle_comments('comment_holder_"+response['streamitem_id']+"');swapcommentlabel(this.id);\">Write a comment...</a></div><div id='streamlike'><a title='Like "+response['first']+" "+ response['middle']+" "+response['last']+"s status' id='likecontext_"+response['streamitem_id']+"' style='cursor:pointer;' onClick=\"likestatus("+response['streamitem_id']+",this.id);\"><div style='width:50px;' id='likesprint"+response['streamitem_id']+"'>Like</a></div><div style='width:50px;' id='likesprint"+response['streamitem_id']+"'></div></div></form></div><div id='streamdislike'><a id='dislikecontext_"+response['streamitem_id']+"' style='cursor:pointer;' onClick=\"dislikestatus("+response['streamitem_id']+",this.id);\"><div style='width:70px;' id='dislikesprint"+response['streamitem_id']+"'>Dislike</a></div><div style='width:70px;' id='dislikesprint"+response['streamitem_id']+"'></div></div></form><div class='stream_comment_holder' style='display:none;' id='comment_holder_"+response['streamitem_id']+"'><div id='comment_list_"+response['streamitem_id']+"'></div><table width=100%><tr><td valign=top width=30px><div class='form'><form id='mycommentform' method='POST' class='form_statusinput'><input type='hidden' name='streamidcontent' id='streamidcontent' value='"+response['streamitem_id']+"'><input type='input' name='commentingcontents' id='commentingcontents' placeholder='Say something' autocomplete='off'><input type='submit' id='button' value='Feed'><br/></div></div>"); 
       } 
     }); 
    } 
</script> 

服务器端:

$lastmodif = isset($_POST['timestamp'])?$_POST['timestamp']:0; 
$last_msg_id = isset($_POST['last_msg_id']) ? $_POST['last_msg_id'] : 0; 


$final_response = array(); 
$check = mysqli_query($mysqli,"SELECT streamitem_content,streamitem_timestamp FROM streamdata WHERE streamitem_id>'$last_msg_id' AND streamitem_status='1' AND streamitem_target=".$_SESSION['id'].""); 
$resultArr = mysqli_fetch_array($check); 
$checks= mysqli_query($mysqli,"SELECT MAX(streamitem_id) AS max FROM streamdata WHERE streamitem_target=".$_SESSION['id'].""); 
$row = mysqli_fetch_array($checks); 
$last_msg_id_db = $row['max']; 

if($last_msg_id_db>$last_msg_id) 
{ 
    $final_response['streamitem_id'] = $last_msg_id_db['streamitem_id']; 
    $final_response['streamitem_content'] = $last_msg_id_db['streamitem_content']; 
    $final_response['streamitem_timestamp'] = Agotime($last_msg_id_db['streamitem_timestamp']); 

    $check1 = mysqli_query($mysqli,"SELECT * FROM users WHERE id=".$_SESSION['id'].""); 
    $resultArr = mysqli_fetch_array($check1); 
    $final_response['username'] = $resultArr['username']; 
    $final_response['id'] = $resultArr['id']; 
    $final_response['first'] = $resultArr['first']; 
    $final_response['middle'] = $resultArr['middle']; 
    $final_response['last'] = $resultArr['last']; 

    echo json_encode($final_response); 

} 
+0

我们可以看到你的客户端代码吗? – halfer

+0

当然。 1时刻 – dave

+0

$ resultArr = mysqli_fetch_array($ check1); SB mysqli_fetch_row? – Cups

回答

0

这可能是部分的事实,$last_msg_id始终是0或1,当你做你做对比。

你应该第一行改成这样:

$last_msg_id = isset($_POST['last_msg_id']) ? $_POST['last_msg_id'] : 0; 

,它将从POST的last_msg_id或将其默认为0,如果还没有设置。

+0

在这种情况下,他将如何获得“第4号”? – Sahal

+0

我认为问题是与json解析 –

+0

@Sahal不知道,从第一篇文章后,该帖子中的代码已更新,但它是不正确的(直接调用'isset()'设置变量)。 –