PHP - Simple Visitor Count Using MySQLi

In this tutorial we will create a Simple Visitor Count using MySQLi. PHP is a server-side scripting language designed primarily for web development. SQLite provides an interface for accessing the database. It includes class interfaces to the SQL commands. And also it allows you to create SQL functions and aggregate using PHP. So Let's do the coding...

Getting started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2.         $conn = mysqli_connect('localhost', 'root', '', 'db_count');
  3.        
  4.         if(!$conn){
  5.                 die("Error: Failed to connect to database!");
  6.         }
  7. ?>
tut1

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into you text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.         </head>
  7. <body>
  8.         <nav class="navbar navbar-default">
  9.                 <div class="container-fluid">
  10.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11.                 </div>
  12.         </nav>
  13.         <div class="col-md-3"></div>
  14.         <div class="col-md-6 well">
  15.                 <h3 class="text-primary">PHP - Simple Visitor Count Using MySQLi</h3>
  16.                 <hr style="border-top:1px dotted;"/>
  17.                 <h4>Please refresh the browser to count</h4>
  18.                 <h3>Visitor have accessed this website <label class="text-primary"><?php include 'counter.php'?></label> times</h3>
  19.         </div>
  20. </body>
  21. </html>

Creating the Main Function

This code contains the main function of the application. This code will start counting when the visitor visit your page. To do this just copy and write these block of codes as shown below inside the text editor and save it as counter.php.
  1. <?php
  2.         require 'conn.php';
  3.        
  4.         $query = mysqli_query($conn, "SELECT * FROM `count") or die(mysqli_error());
  5.         $fetch = mysqli_fetch_array($query);
  6.         $rows = mysqli_num_rows($query);
  7.        
  8.         if($rows == 0){
  9.                 mysqli_query($conn, "INSERT INTO `count` VALUES('', '')") or die(mysqli_error());
  10.         }
  11.        
  12.         $count = $fetch['counts'] + 1;
  13.         mysqli_query($conn, "UPDATE `count` SET `counts` = '$count'") or die(mysqli_error());
  14.         echo $count;
  15.        
  16.        
  17. ?>
There you have it we successfully created a Simple Visitor Count using MySQLi. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!

Add new comment