0

我的数据库中有一张桌子。

Employee
___________________________________
| Name      |   City              |    
___________________________________
| Jack      |  UK                 |
___________________________________
| John      |  Kenya              |
___________________________________

我正在使用选择查询从该表中获取值。
这是我的 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'] . "&nbsp" . $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,否则什么都没有。
我怎样才能做到这一点。
谢谢。

4

1 回答 1

1

您应该将轮询间隔从 1 秒增加到 15 秒,这样就可以了。


一个真正的通知系统——比如在 StackExchange 上——需要一个基于Web 套接字的解决方案。此外,您还需要 mysql 中的一些存储过程 - 注册为触发函数 - 可以与 Web 套接字服务器通信。

于 2013-08-18T05:54:00.460 回答