count.php
$sql=$db->prepare("SELECT * FROM table WHERE selector=:selector");
$sql->execute(array(':selector'=>$selector));
$count=$sql->rowCount();
$arr = array('count' => $count)
echo json_encode($arr);
添加下面的脚本到你的索引文件
<script>
$(document).ready(function() {
setInterval("ajaxcall()",2000);
});
function ajaxcall() {
$.ajax({
type: "GET",
url: "count.php"
success: function(response){
json_object = JSON.parse(response)
var count = json_object.count
/// set this count variable in element where you want to
/// display count
}
});
}
</script>
如果你不想使用jQuery比你可以试试:
function ajaxcall() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
json_object = JSON.parse(xhttp.responseText)
var count = json_object.count
/// set this count variable in element where you want to
/// display count
}
};
xhttp.open("GET", "count.php", true);
xhttp.send();
setTimeout(ajaxcall, 1000)
}
</script>
而且在您的身体标记上添加加载事件
<body onload="setTimeout(ajaxcall, 3000)" >
,或者您可以拨打阿贾克斯一些按钮,点击
<button onclick="setTimeout(ajaxcall, 3000)">clickme</button>
使用的setInterval()和每x mintues调用使用Ajax数据库。你的代码是 – msvairam