PHP AJAX Comet 与 MySQL 选择记录

标签 php jquery html mysql ajax

我想通过从 PHP 获取记录来实现 comet

我的 PHP 将在页面调用 getlog.php

时执行以下操作。
$sql = "select log_description,log_time from log ORDER by log_time DESC";

$result=mysql_query($sql);
if($result == false)
{     die("unable to fetch records."); }

while ($row = mysql_fetch_assoc($result)) {
   $result_output[] = $row;
}

$counter = 1;
foreach($result_output as $row)
{
echo $counter . ".  " $row[log_description];
$counter++;
}

如果有新日志,我想在viewlog.php中回显它

所以它会在viewlog.php中显示为这样

1. Customer 1 logged in at 12:05.

也许5分钟后

1. Customer 2 logged in at 12:10
2. Customer 1 logged in at 12:05

它最多维护 15 条记录。

数据是从 PHP 获取的,我读到的方法是所谓的“comet”,但我只想要一个简单的数据库获取,它会自动刷新,例如每 10 秒刷新一次,看看是否有新记录添加到数据库中,将其附加到 div。

有没有一种简单的方法可以使用 AJAX 和 PHP 而不是使用 comet 来实现此目的。

感谢大家的帮助,非常感谢!

以下代码是否已更改

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>

<script>
   show_log(){
    var lnk = "fetchlog.php";
    $.ajax({url:lnk,success:function(result){
        $("#log_div").html(result);
    }});
}
   window.setInterval(function(){
  show_log();
}, 10000);
    </script>

</head>
<body>
<div id="log_div"></div>
</body>
</html>

我的代码有什么问题,因为它没有从 fetchlog.php 获取

fetchlog.php 回显类似这样的内容

1. Acct_1 logged to the system.
2. Acct_3  logged in to the system.
3. Acct_2  logged in to the system.
4. Assign permissions on Acct_1.
5. Delete record on table building with id 80

jsFiddle

最佳答案

是的,您可以使用 ajax 来实现此目的,只需更新 html 中的 div 即可。 您需要链接 jquery 才能使用下面的代码。

show_log(){
    var lnk = "link to the viewlog.php file";
    $.ajax({url:lnk,success:function(result){
        $("#log_div").html(result);
    }});
}

每 x 分钟运行一次 show_log() 函数。 让 viewlog.php 按时间降序显示最后 x 条记录。 您可以将 sql 更新为如下所示

$sql = "select log_description,log_time from log ORDER by log_time DESC LIMIT 5 ";

您可以在 JavaScript 中使用以下代码每隔 x 秒运行该函数。每 10 秒一次。

window.setInterval(function(){
  show_log();
}, 10000);

10,000 的单位是毫秒

-----尝试下面的

<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>

<script>
http = getHTTPObject();

function getHTTPObject(){
  var xmlhttp;

  if(!xmlhttp && typeof XMLHttpRequest != 'undefined'){
    try {
      xmlhttp = new XMLHttpRequest();
    }catch(e){
      xmlhttp = false;
    }
  }
  return xmlhttp;
}
function   show_log(){
    var url = "viewlog.php";
  http.open("GET", url, true);
  http.onreadystatechange = handleHttpResponse;
  http.send(null);
}
function handleHttpResponse(){
  if(http.readyState == 4){
    document.getElementById('log_div').innerHTML = http.responseText;
  }
}
   setInterval ( "show_log()", 5000 );
    </script>

</head>
<body>
<div id="log_div"></div>
</body>
</html>

关于PHP AJAX Comet 与 MySQL 选择记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19922438/

相关文章:

jquery - Material 设计精简版(MDL)中的水平和垂直中心卡

php - HTML/PHP 表单未发布 (MYSQL)

php - Cron 作业无法使用 crontab linux

PHP:显示单个数组数据?

php - HTML 表格到 XLS(用于公式的单元格)

jquery - 需要像 functionailty 这样的 gmail - jquery 自动完成以包含姓名和电子邮件地址 - 在字符串搜索中

Jquery json 响应变量值

javascript - 事件传播和冒泡

html - li 内的 CSS 垂直对齐文本

php - 如果其中一列大于时间戳,Mysql 按两列排序