PHP Mysql Visitor Counter Script
Mysql PHP PHP Scripts Sample Programs

PHP Mysql Visitor Counter Script

If you want to add a counter to a web page or a website than use below simple PHP Counter Script using MySql. It is very easy to implement it and it counts whenever a page is being refreshed and the script wont reduce page speed unlike page counter scripts.

PHP Visitor Counter Script

PHP Mysql Visitor Counter Script

PHP Counter Script

First Create a table called Counter and add below MySql Queries.

CREATE TABLE `Counter` (
  `visits` int(15) NOT NULL,
  PRIMARY KEY (`id`)

INSERT INTO `Counter` (`id`, `visits`) VALUES
(1, 1);

PHP Script for Counter

Now Create a PHP file and name it is as db.php

In db.php file add below code.


$dbhost = "localhost";
$dbuser = "admin";
$dbpass = "password";
$dbname = "databaseName";

$conn = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname);


In place of dbhost, dbuser, dbpass, dbname add your Database details.

Now create a PHP file called counter.php and add below code in it.


include 'db.php';

$sql = "UPDATE Counter SET visits = visits+1 WHERE id = 1";
$result = mysqli_query($conn, $sql);

$sql = "SELECT visits FROM Counter WHERE id = 1";
$result = mysqli_query($conn, $sql);

if ($result->num_rows > 0) {
	while($row = $result->fetch_assoc()) {
		$visits = $row["visits"];
} else {
	echo "No Visits";

echo "Total Number of Visits: ". $visits;

Now upload both the db.php and counter.php on your web hosting and load the counter.php file to see the visitors count.

That’s it its a simple PHP MySql Counter Script that doesn’t reduce page speed unlike third-party visitor or counter scripts.