0

所以,这是我的代码:

<?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);  
?>

我怎样才能使它每隔几秒钟查询一次数据库的计数?

4

1 回答 1

4

单独保存您的 PHP 脚本(在我的示例中位于 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:

 <script>
    $(function(){
      function loadNum()
      {  
        $('h1.countdown').load('thescript.php');
        setTimeout(loadNum, 5000); // makes it reload every 5 sec
      }
      loadNum(); // start the process...
    });
 </script>
于 2012-11-20T23:05:20.990 回答