簡體   English   中英

如何每隔幾秒鍾查詢一次數據庫?

[英]How can I query the database every few seconds?

所以,這是我的代碼:

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

我怎樣才能使它每隔幾秒鍾就查詢數據庫的計數?

單獨保存您的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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM