我在让我的php做我想做的事情上遇到了真正的问题。我正在尝试长时间轮询服务器并输出以下所有信息。
可以看一看,看看我做错了什么。我只是需要它来输出正确的详细信息..在服务器端streamitem_id
和content
输出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 :(得分:0)
这可能与您进行比较时$last_msg_id
始终为0或1的事实有关。
您应该将第一行更改为:
$last_msg_id = isset($_POST['last_msg_id']) ? $_POST['last_msg_id'] : 0;
这将从POST中取last_msg_id
,如果未设置则将其默认为0.