简体   繁体   中英

How can I query the database every few seconds?

So, this is my code:

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

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; 

?>

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM