topical media & game development

talk show tell print

server-php-sqlite-count.php / php



  <?php
  
  // create a SQLite3 database file with PDO and return a database handle (Object Oriented)
  try{
  
  //echo 'sqlite:'._SERVER['DOCUMENT_ROOT'].'/www/hush/sql/test.db';
  //dbHandle = new PDO('sqlite:'._SERVER['DOCUMENT_ROOT'].'/hush/sql/test.db');
  dbHandle = new PDO('sqlite:server-php-sqlite-count.db');
  
  }catch( PDOException exception ){
  
  die(exception->getMessage());
  
  }
  
  // create page view database table
  sqlCreateTable = 'CREATE TABLE pageView(id INTEGER PRIMARY KEY AUTOINCREMENT, page CHAR(256), access INTEGER(10))';
  dbHandle->exec(sqlCreateTable); // remove or 'comment out' this line after first run
  
  // insert page visit in database with a prepared statement
  sqlInsertVisit = 'INSERT INTO pageView (page, access) VALUES (:page, :access)';
  
  stmt = dbHandle->prepare(sqlInsertVisit);
  stmt->bindParam(':page', _SERVER['PHP_SELF'], PDO::PARAM_STR);
  stmt->bindParam(':access', time(), PDO::PARAM_INT);
  stmt->execute();
  
  // get page views from database
  pageVisit = dbHandle->quote(_SERVER['PHP_SELF']);
  sqlGetView = 'SELECT count(page) AS view FROM pageView WHERE page = '.pageVisit.'';
  result = dbHandle->query(sqlGetView);
  pageView = result->fetch(); // store result in array
  
  // print page views
  echo pageVisit.' page has been viewed '.pageView['view'].' times.';
  
  ?>
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.