title: Implementing long polling on the server side based on my code
body: <p>I already asked about this subject on how can I convert my basic polling script into a long polling structure and a user from <a href="https://stackoverflow.com/questions/53991213/from-polling-to-long-polling">that post</a> said there is two parts to long polling. The client side structure and the server side </p>
  
  <p>structure and he suggested I reask this question but this time referencing more on how I can implement long polling on the server side. Since he already provided the long polling structure example in the client side by JavaScript. So how can I implement long polling on the server side for the file called </p>
  
  <p><em>check-for-new-records.php</em>? </p>
  
  <p>Here is the rest of my code to better understand what I mean.</p>
  
  <p><strong>index.php</strong></p>
  
  <pre><code>&lt;style&gt;
  #label{
      margin: 0;
      color: red;
      font-weight: bold;
    }
  &lt;/style&gt;
  
  &lt;script&gt;
  
  document.addEventListener('DOMContentLoaded',function(){
  
  /**********************************************************************
  Check for a new record amount in the data base
  **********************************************************************/    
  
  //Client side structure of long-polling
  
  function checkForNewRecords() {
  
  var xhr = new XMLHttpRequest();
  
  xhr.onreadystatechange = function() {
  
      if (xhr.readyState == 4) {
      document.querySelector('#output').innerHTML = xhr.responseText;
  
      setTimeout(checkForNewRecords, 0);
    }
  }
  
  xhr.open('POST', 'check-for-new-records.php');
  xhr.send();
  
  }
  
  checkForNewRecords()
  
  });
  
  &lt;/script&gt;
  
  &lt;p id='label'&gt;Total records in the database in real time in basic polling&lt;/p&gt;
  
  &lt;div id='output'&gt;&lt;/div&gt;
  </code></pre>
  
  <p><strong>check-for-new-records.php</strong></p>
  
  <pre><code>&lt;?php
  
  //Long polling in the server side code... How???
  
  $db_servername= 'localhost';
  $db_username='jd';
  $db_password= '1234';
  $db_name= 'test';
  
  $db_connect= new mysqli($db_servername,$db_username,$db_password,$db_name);
  
  $db_query= "SELECT COUNT(id) AS id FROM example";
  
  $db_result= $db_connect-&gt;query($db_query);
  $db_row= $db_result-&gt;fetch_object();
  
  $total_records= $db_row-&gt;id;
  
  ?&gt;
  
  &lt;style&gt;
  #total-records{
  margin-top: 0;
  }
  &lt;/style&gt;
  
  &lt;p id='total-records'&gt;&lt;?php echo $total_records; ?&gt;&lt;/p&gt;
  </code></pre>