Make simple page counter using php and mysql - Mostlikers

06 July, 2013

Make simple page counter using php and mysql

A hit counter or a page counter counts the number of people that visit your page. If you have created a new website you might be interested how many people are visiting. so try this simple method count how many people visit on your website. this coding make MySQL update query using in php.




Database

Create count table and insert sample value in name field to the database. Ex; Name="Mostlikers" don't insert count value watch below table coding


CREATE TABLE Count
(
id INT  AUTO_INCREMENT,
Name TEXT
count INT
);

Count.php

Create count.php page Fetch database count table value from this page using below coding update through 
page count value will be increased.

<?php
$fetch=mysql_query("select * from count");
while($row1=mysql_fetch_array($fetch))
{

    $id=$row1['id'];
    $samplename=$row1['name'];
    $counts=$row1['count'];
    $tolcount2=$counts+1;
    $updates=mysql_query("update count set count='$tolcount2' where id='$id' ");
}
?>

Fetch the count value using increment value +1 to update the database from given value

out.php

Refresh count.php file value will be increased show as output below

 <span style="color:#F00;">(<?php echo $tolcount2; ?>)</span>

4 comments:

  1. For this you don't need a MySQL DB... a simple textfile is fully sufficient ;)

    ReplyDelete
  2. How to make it using text file...???

    ReplyDelete
  3. hi db :
    $hostname_visitors = "localhost";
    $database_visitors = "page-count-php";
    $username_visitors = "root";
    $password_visitors = "dbpass";

    $visitors = mysql_connect($hostname_visitors, $username_visitors, $password_visitors) or trigger_error(mysql_error(),E_USER_ERROR)

    ReplyDelete