1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
// create a SQLite3 database file with PDO and return a database handle (Object Oriented)
try{
$dbHandle = new PDO('sqlite:test.sqlite3');
}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 'This page has been viewed '.$pageView['view'].' times.'; |
Partager