使用 AJAX 来自动更新包含数据库值的页面?
有人可以解释如何创建一个使用数据库中的值自动更新的 .php 页面吗?对于这样的事情使用 AJAX 是最好的吗?我想要这段 PHP 代码,但我希望每当添加或更改“values01”时都会更新页面,而无需刷新页面。$query = "从用户中选择值01,其中用户名='$username'";
$结果 = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo nl2br("{$row['values01']}");
}
任何帮助将不胜感激! :)
Can someone explain how to create a .php page which auto-updates with values from a database? Would using AJAX be best for something like this? I want this PHP code, but I want the page to be updated whenever 'values01' is added to or changed, without having to refresh the page.$query = "SELECT values01 FROM users WHERE username='$username'";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo nl2br("{$row['values01']}");
}
Any help would be appreciated! :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您所描述的是来自网络服务器的推送通知。不幸的是,我还没有找到一种可靠的方法来进行传统的推送,即 Web 服务器将发起与客户端的连接(根据我有限的研究,似乎有关于 http 标准支持推送的建议,但是,它并没有看起来还没有出现过广泛采用的标准)。然而,这可以通过更迂回的方式来完成。
有一种方法可以绕过通常使用的推送限制:
在典型的推送通知中,您唯一缺少的就是 10 秒(或您为计时器选择的任何时间)延迟。
What you are describing would be a push notification from the web server. Unfortunately, I have not found a reliable way to do a traditional push, where the web server would initiate the connection with the client (from my limited research, it appears that there were proposals for the http standard to support push, however, it doesn't look like a widely adopted standard ever emerged). However, this can be done in a bit more roundabout way.
There is a way around the push limitation that is typically used:
The only thing you are missing here from a typical push notification, is the 10-second (or whatever you choose for your timer) lag.