我如何每隔几秒钟查询数据库? [英] How can I query the database every few seconds?

查看:667
本文介绍了我如何每隔几秒钟查询数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,这是我的代码:

<?php
//Connect to the database
include("config.php");

//Query the database and get the count  
$result = mysql_query("SELECT * FROM ads");  
$num_rows = mysql_num_rows($result);  
?>

如何使它每秒钟查询数据库的计数?

How can I make it so it query's the database for the count every few seconds?

推荐答案

单独保存PHP脚本(在我的示例下面的thescript.php)

Save your PHP script separately (in my example below thescript.php)

<?php

  //Connect to the database
  include("config.php");

  //Query the database and get the count 
  $q = mysql_query("SELECT count(*) as num FROM ads");
  $count = mysql_fetch_result($q,0,'num');
  echo $count; 

?>

然后在home.php文件中使用AJAX / javascript / jQuery:

Then use AJAX/javascript/jQuery in your home.php file:

 <script>
    $(function(){
      function loadNum()
      {  
        $('h1.countdown').load('thescript.php');
        setTimeout(loadNum, 5000); // makes it reload every 5 sec
      }
      loadNum(); // start the process...
    });
 </script>

这篇关于我如何每隔几秒钟查询数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆