title: Put data from Postgresql database to table using Ajax and PhP
body: <p>The goal of my task is to populate data from postgresql database every 5 seconds using ajax and php. I followed the example <a href="https://stackoverflow.com/questions/25047156/how-to-put-data-from-database-table-to-a-html-table-using-ajax-and-php/52296521?noredirect=1#comment91540919_52296521">How to put data from database table to a html table using ajax and php</a>. I am new in PhP so, I don't know why data is not populating. I did testing of the code and got data in console successfully but not in table. 
  <strong>Client side code</strong></p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;style&gt; &lt;?php include './../css/hardwarestatus.css'; ?&gt; &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div class="maindiv"&gt;
          &lt;div id="tagsDiv"&gt; 
                  &lt;h3&gt;Estat Tags&lt;/h3&gt;
                  &lt;?php  
  
                          echo '&lt;table id="tags_table"&gt;';
                          echo "&lt;tr&gt;";
                                  echo '&lt;th&gt;' . "TAG" . '&lt;/th&gt;';
                                  echo '&lt;th&gt;' . "BATTERY" . '&lt;/th&gt;';
                                  echo '&lt;th&gt;' . " Time " . '&lt;/th&gt;';
                                  echo '&lt;th&gt;' . "Status" . '&lt;/th&gt;';
                          echo "&lt;/tr&gt;";
                          echo '&lt;/table&gt;';                            
                  ?&gt;
          &lt;/div&gt;
  
  &lt;/div&gt;
  
  &lt;script src='http://code.jquery.com/jquery-3.1.1.min.js'&gt;&lt;/script&gt;
  
  &lt;script&gt;
  $(document).ready(function(){
          UpdateHTMLTable();
          setInterval(function() {
                  UpdateHTMLTable();
          }, 5000); // 5000 millisecond(5 second)
  
  function UpdateHTMLTable(){
          var xmlhttp = new XMLHttpRequest();
          xmlhttp.onreadystatechange = function() {
          if (this.readyState == 4 &amp;&amp; this.status == 200) {
  
  
              myObj = JSON.parse(this.responseText);
  
  
              for (i = 0; i &lt; myObj.length; i++) {
  
                  console.log(myObj[i]);
  
                  var row = $("&lt;tr /&gt;");
                      $("&lt;td /&gt;").text(myObj[i].mac).appendTo(row);
                      $("&lt;td /&gt;").text(myObj[i].battery).appendTo(row);
                      $("&lt;td /&gt;").text(myObj[i].ts).appendTo(row);
                      $("&lt;td /&gt;").text(myObj[i].ts).appendTo(row);
                      row.appendTo('#tags_table');     
                  } 
          }
  };
  xmlhttp.open("GET", "pages/estat_hardware_server.php", true);
  xmlhttp.send();
  }
  });
  &lt;/script&gt;
  
  &lt;/body&gt;
  &lt;/html&gt; 
  </code></pre>
  
  <p><strong>On server side, my code is</strong></p>
  
  <pre><code>&lt;?php 
  header("Content-Type: application/json; charset=UTF-8");  
          $host = ip of the host";
          $port ="port no."; 
          $user = "user"; 
          $pass = "123"; 
          $db = "db_name"; 
          $con = pg_connect("host=$host dbname=$db user=$user password=$pass")
          or die ("Could not connect to server\n"); 
          $query = "Query string"; 
          $result = pg_query($con, $query) or die("Cannot execute query: $query\n");
          if(pg_num_rows($result))
          {
              $data=array();
              while($row=pg_fetch_array($result))
              { 
                  $data[] = array(
                  'mac'=&gt;$row['mac'],
                  'ts' =&gt;$row['ts'],
                  'battery'=&gt;$row['battery']
                   );
              }         
              echo  json_encode($data);
              pg_free_result($result);
              pg_close($con);
          }                                
  ?&gt;
  </code></pre>