我从一个页面和一个php服务器端页面(server.php)发送了许多动态帖子id,使用这些ID进行查询以查找mysql中新添加的数据。
如果在 mysql 中找不到任何新添加的数据,则返回一个undefined
值。因此,根据我的脚本,它会以一个接一个的时间间隔附加一个undefined
。
那么我该如何检查,如果 php 查询在 sql 中找不到任何内容,然后退出而不返回任何内容?
我在我的 php if(mysqli_num_rows($res)) { //do something }
中尝试过这个,但它也显示未定义。
我的JavaScript:
var CID = []; // Get all dynamic ids of posts (works well)
$('div[data-post-id]').each(function(i){
CID[i] = $(this).data('post-id');
});
function addrep(type, msg){
CID.forEach(function(id){
$("#newreply"+id).append("<div class='"+ type +""+ msg.id +"'><ul><div class='cdomment_text'>"+ msg.detail +"</ul></div>");
});
}
function waitForRep(){
$.ajax({
type: "GET",
url: "server.php",
cache: false,
data: {CID : CID},
timeout:15000,
success: function(data){
addrep("postreply", data);
setTimeout(waitForRep, 15000 );
},
error: function(XMLHttpRequest, textStatus, errorThrown){
setTimeout(waitForRep, 15000); }
});
}
$(document).ready(function(){
waitForRep();
});
服务器.php
while (true) {
if($_REQUEST['CID']){ //cid got all dynamic post id as: 1,2,3,4 etc.
foreach($_REQUEST['CID'] as $key => $value){
$datetime = date('Y-m-d H:i:s', strtotime('-15 second'));
$res = mysqli_query($dbh,"SELECT * FROM reply WHERE qazi_id=".$_REQUEST['tutid']." AND date >= '$datetime' ORDER BY id DESC LIMIT 1") or die(mysqli_error($dbh));
$data = array();
while($rows = mysqli_fetch_assoc($res)){
$data[]=$rows;
$data['id'] = $rows['id'];
$data['qazi_id'] = $rows['qazi_id'];
$data['username'] = $rows['username'];
$data['description'] = $rows['description'];
$data['date'] = $rows['date'];
//etc. all
$id = $rows['id'];
$qazi_id = $rows['qazi_id'];
$username = $rows['username'];
$description = $rows['description'];
//etc. all
} //foreach close
} //foreach close
if ($description=="") {$detail .= '';}
else {$detail .=''.$description.'';}
$data['detail'] = $detail;
// do others something more
if (!empty($data)) {
echo json_encode($data);
flush();
exit(0);
}
} //request close
sleep(5);
} //while close
我在 php if(mysqli_num_rows($res)) { //do something }
中尝试过这个,但它也显示undefined
.
我想这应该是因为您每隔 15000
毫秒通过 ajax 调用一次此 php 代码,并带有setTimeout
.
因此,您可以将其停在那里,而是在函数中使用js代码忽略它addrep()
。
function addrep(type, msg) {
CID.forEach(function(id) {
if (msg.id !== undefined && msg.detail !== undefined) { // <--check undefined here
$("#newreply" + id).append("<div class='" + type + "" + msg.id + "'>"+
"<ul><div class='cdomment_text'>" + msg.detail +
"</ul></div>");
}
});
}
或者其他选择是在undefined
时利用clearTimeout()
。
var timer; // declare the timer here
var CID = []; // Get all dynamic ids of posts (works well)
$('div[data-post-id]').each(function(i) {
CID[i] = $(this).data('post-id');
});
function addrep(type, msg) {
CID.forEach(function(id) {
if(msg.id === undefined || msg.details === undefined){
clearTimeout(timer); // cleartimeout timer
}else{
$("#newreply" + id).append("<div class='" + type + "" + msg.id + "'><ul><div class='cdomment_text'>" + msg.detail + "</ul></div>");
}
});
}
function waitForRep() {
$.ajax({
type: "GET",
url: "server.php",
cache: false,
data: {
CID: CID
},
timeout: 15000,
success: function(data) {
addrep("postreply", data);
timer = setTimeout(waitForRep, 15000); // assign the settimeout to timer
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
setTimeout(waitForRep, 15000);
}
});
}
$(document).ready(function() {
waitForRep();
});