title: live visitor counter for single page
body: <p>I'm current working on a live leader-board page and i am trying to add a live view counter to it. I am not getting and data sent to the database and its not reading from the database. I have checked the db connection and its working. I have checked the code in php tester and it has no errors.</p>
  
  <p>Take a look and let me know what i missed, Thanks</p>
  
  <pre><code>class VisitorCounterReal {
  
        var $sessionTimeInMin = 5; // time session will live, in minutes
  
   function VisitorCounter() {
      $ip  = $_SERVER['REMOTE_ADDR'];
  
      $this-&gt;cleanVisitors();
  
      if ($this-&gt;visitorExists($ip))
      {
          $this-&gt;updateVisitor($ip);
      } else
      {
          $this-&gt;addVisitor($ip);
      }
  
  
      }
  
      function visitorExists($ip) {
  
           $query = "SELECT * FROM counter WHERE ip = '$ip'";
           $res = mysqli_query($connection, $query);
           if (mysqli_num_rows($res) &gt; 0)
  
      {
           return true;
      } else
           if (mysqli_num_rows($res) == 0)
          {
              return false;
          }
      }
  
      function cleanVisitors()
      {
      $sessionTime = 30;
      $query = "SELECT * FROM counter";
      $res = mysqli_query($connection, $query);
      while ($row = mysqli_fetch_array($res))
      {
          if (time() - $row['lastvisit'] &gt;= $this-&gt;sessionTimeInMin * 60)
          {
              $dsql = "delete from counter where id = $row[id]";
              mysqli_query($dsql);
          }
      }
      }
  
  
      function updateVisitor($ip)
      {
  
      $query = "UPDATE counter SET lastvisit = '" . time() . "' WHERE ip =
      '$ip'";
      mysqli_query($connection, $query);
      }
  
  
      function addVisitor($ip)
      {
      $query = "INSERT INTO counter(ip, lastvisit) ";
      $query .= "VALUES('$ip', '" . time() . "') ";
      mysqli_query($connection, $query);
      }
  
      function getAmountVisitors()
      {
  
      $query = "SELECT COUNT(id) FROM counter";
  
      $res = mysqli_query($connection, $query);
  
      $row = mysqli_fetch_row($res);
  
      return $row[0];
      }
  
  
      function show()
      {
  
      echo '&lt;h3&gt;There is ' . $this-&gt;getAmountVisitors() . ' watching 
      online&lt;/h3&gt;';
  
  
  
      }
  
      }
  </code></pre>
  
  <p>This is on the live leaderboard page below</p>
  
  <pre><code>&lt;?php
  
      $counter = new VisitorCounterReal; // make a new counter
      //content here
      $counter-&gt;show(); // show the counter
      // and here
  ?&gt;
  </code></pre>