title: page view counter, code is provided, is it the best way?
body: <p>lets assume we have this table called <code>visits</code> and it has two fields <code>id</code> for the page id and <code>ip</code> for the user's ip address.  </p>
  
  <p>I have written this code and I'm wondering if it's the best way?</p>
  
  <pre><code>    $ip=$_SERVER['REMOTE_ADDR'];
      $id=$_GET['id'];
  
      $query = "SELECT
      (SELECT COUNT(ip) FROM visits WHERE id = '{$id}' AND WHERE ip ='{$ip}') as visited,
      (SELECT COUNT(ip) FROM visits WHERE id = '{$id}') as pageHits";
  
      $result=mysql_query($query, $connection);
      $row=mysql_fetch_array($result);
  
      $pageHits=$row['pageHits'];
      $visited=$row['visited']; //it's either 0 or 1;
  
      if($visited==0){
          $query ="INSERT INTO visits (ip , id) VALUES ('{$ip}', {$id})"
          $result=mysql_query($query, $connection);
          $pageHits++;
      }
  
      echo $pageHits;
  </code></pre>