我的数据库中有一个表。
Employee
___________________________________
| Name | City |
___________________________________
| Jack | UK |
___________________________________
| John | Kenya |
___________________________________
我使用选择查询从此表中获取值
这是来自Employee的选择数据的sql.php文件。
<?php
$con=mysqli_connect("localhost","root","","company");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM employee");
while($row = mysqli_fetch_array($result))
{
echo $row['name'] . " " . $row['city'];
}
mysqli_close($con);
?>
现在我试着长时间拉这是长拉的简单代码: -
<html>
<head>
<title>BargePoller</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<style type="text/css" media="screen">
body{ background:#000;color:#fff;font-size:.9em; }
.msg{ background:#aaa;padding:.2em; border-bottom:1px #000 solid}
.old{ background-color:#246499;}
.new{ background-color:#3B9957;}
.error{ background-color:#992E36;}
</style>
<script type="text/javascript" charset="utf-8">
function addmsg(type, msg){
/* Simple helper to add a div.
type is the name of a CSS class (old/new/error).
msg is the contents of the div */
$("#messages").append(
"<div class='msg "+ type +"'>"+ msg +"</div>"
);
}
function waitForMsg(){
/* This requests the url "msgsrv.php"
When it complete (or errors)*/
$.ajax({
type: "GET",
url: "sql.php",
async: true, /* If set to non-async, browser shows page as "Loading.."*/
cache: false,
timeout:1000, /* Timeout in ms */
success: function(data){ /* called when request to barge.php completes */
addmsg("new", data); /* Add response to a .msg div (with the "new" class)*/
setTimeout(
waitForMsg, /* Request next message */
1000 /* ..after 1 seconds */
);
},
error: function(XMLHttpRequest, textStatus, errorThrown){
addmsg("error", textStatus + " (" + errorThrown + ")");
setTimeout(
waitForMsg, /* Try again after.. */
1000); /* milliseconds (15seconds) */
}
});
};
$(document).ready(function(){
waitForMsg(); /* Start the inital request */
});
</script>
</head>
<body>
<div id="messages">
<div class="msg old">
BargePoll message requester!
</div>
</div>
</body>
</html>
它的工作完美,ajax呼叫每一秒
但我想设置如果在表中进行任何更改,那么它在我的页面上的附加div其他明智的没有。
我怎么能这样做。
感谢。
答案 0 :(得分:1)
你应该将轮询间隔从1秒增加到15秒,你没事。
真实的通知系统 - 就像在StackExchange上一样 - 需要基于web socket的解决方案。另外,你需要在mysql中存储一些存储过程 - 注册为触发器函数 - 它们可以与Web套接字服务器通信。