每15秒运行一次PHP,并在数据库更改时通知用户

时间:2012-04-05 10:42:42

标签: php jquery database mobile

我正在使用JQuery Mobile构建应用程序。我有一个名为list.php的页面,它使用PHP从服务器获取数据。我需要脚本每15秒运行一次,如果数据库中有更改,则通知用户。我是PHP和Javascript的初学者,所以我不知道如何做到这一点,或者甚至可能。

这是我目前正在使用的代码。

<?php
$number = $_GET['number'] ;

mysql_connect('localhost','username','password');
$link = mysql_connect('localhost', 'username', 'password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}


//specify database 
mysql_select_db("database") or die("Unable to select database"); 

// Build SQL Query 
$query = "select * from table where tablenumber = \"$number\" and state = 0  
  order by time";

 $result = mysql_query($query) or die("Couldn't execute query");

?>





<!DOCTYPE html> 
<html> 
    <head> 
    <title>title</title> 

    <meta name="viewport" content="width=device-width, initial-scale=1"> 

    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.css" />
    <script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.js"></script>

</head> 
<body> 


<div data-role="page">

    <div data-role="header" > 
    <h1>Transpanel</h1>
</div>
    <div data-role="content">   


        <div data-role="collapsible-set">

    <?php 
// display the results returned
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>
<div data-role="collapsible" data-theme="b" data-content-theme="d">
   <h3><?= $row["OSO"] ?></h3>
   <p><?=$row["AIKA"]?><p>
</div>
<?php } ?>



</div>


    </div><!-- /content -->

</body>
</html>

6 个答案:

答案 0 :(得分:1)

只需使用AJAX,并按setInterval('checkfornew()', 15000);每15秒调用一次AJAX函数。 所以:

function checkfornew() {
    var http = new XMLHttpRequest();
    var url = "get.php";

    http.open("GET", url, true);

    //Send the proper header information along with the request
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
    http.setRequestHeader("Content-length", params.length);
    http.setRequestHeader("Connection", "close");

    http.onreadystatechange = function() {
        if(http.readyState == 4 && http.status == 200) {
            if(http.responseText != "") {
                alert(responseText);
            }
        }
    }
    http.send(params);
}

并将body-tag更改为

<body OnLoad="setInterval('checkfornew()', 1500);">

答案 1 :(得分:0)

为什么不修改更新逻辑,以便启动通知用户的通知线程?以这种方式进行轮询是非常低效的。

答案 2 :(得分:0)

如果您在Linux机器上,请将脚本添加到cronjob

评论后编辑

# crontab -e
00 * * * * /usr/local/bin/php /home/john/myscript.php

或者如果它在URL上

 00 * * * * lynx -dump http://www.thegeekstuff.com/myscript.php

答案 3 :(得分:0)

服务器上的代码无法自行运行并通知浏览器更改,它只是没有内置到HTTP协议中。

一个简单的解决方案是每15秒重新加载一次页面:

<body onload="window.setTimeout(function(){document.location.reload(true);},15000)">

如果您希望浏览器在不重新加载页面的情况下获取更新,您应该查看AJAX。基本上,您设置了另一个可以返回更改的PHP页面,例如以JSON格式。在页面中使用Javascript(例如jQuery库中的ajax方法),您可以获取更改并让脚本对它们作出反应。

答案 4 :(得分:0)

为了每15秒更新一次,您最好使用cron作业。 为了在数据库发生变化时通知用户,这是我的解决方案: 创建另一个表,其中包含用户所执行操作的记录。 另一个选择是在db上实现哈希函数,但我不知道它的效率如何。

答案 5 :(得分:0)

我认为您正在尝试创建移动网络应用。通过setTimeout()每15秒加载整个页面可能会给您的浏览器带来不必要的负担,就像每隔15秒点击浏览器刷新按钮一样烦人!相反,您需要做的就是使用json Web服务并在php-json方法中解析jquery.getJSON响应。 使用2个不同的文件:list.html for jquery&amp; list.php为json-web-services。 下面是一个用响应 结果解析json的示例:{key1:OSO; key2:AIKA}

$(document).ready(){ 
//always include jquery code in $(document).ready(){ your javascript here} loads faster
setInterval(function() {
    $.getJSON('list.php', function(json) {
    //assuming list.php resides with list.html directory
    //console.log(json)
      console.log(json.result.key1);
       console.log(json.result.key2);

    });
  }, 15000); //request list.php every 15 seconds
});
相关问题