Advertisement
mkv

simple hit counter

mkv
Jan 23rd, 2015
455
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.91 KB | None | 0 0
  1. <?php
  2.  
  3. // Create a connection to the database
  4. $database = new mysqli('localhost', 'username', 'password', 'databasename') or die('Could not connect to database');
  5.  
  6. // Increase the hitcounter by one
  7. $database->query('update hitcounter set hits = hits + 1') or init();
  8.  
  9. // Do a mysql "search" to get the hitcounter
  10. $result = $database->query('select hits from hitcounter') or die('error selecting hitcounter');
  11.  
  12. // Read the result
  13. if ($result = $result->fetch_assoc())
  14. {
  15.     // Show the hitcount
  16.     echo $result['hits'];
  17. }
  18. else init();
  19.  
  20.  
  21.  
  22. // Called if the increase step fails
  23. function init()
  24. {
  25.     global $database;
  26.  
  27.     // Could not increase hitcounter, probably the table doesn't exist
  28.     $database->query('create table if not exists hitcounter ( hits int(11) not null )') or die('could not create table');
  29.     $database->query('insert into hitcounter values(1)') or die('could not insert hitcounter');
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement