PHP Counter Script Tutorial

April 5, 2011, PHP

Creates your own counter script using a basic knowledge of php + mysql script and keeps your visitors number to database for analysis.

Step1:

Create table “counter”

CREATE TABLE `counter` (
`counter` int(9) NOT NULL default ’0′
) TYPE=MyISAM;

grey PHP Counter Script Tutorial

Step2:

Create file counter.php

|—|——————— code ——————–|—|

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
<p><?php<br>
$host="localhost"; // Host name <br>
$username=""; // Mysql username <br>
$password=""; // Mysql password <br>
$db_name="test"; // Database name <br>
$tbl_name="members"; // Table name </p>
<p>// Connect to server and select database.<br>
  mysql_connect("$host", "$username", "$password")or die("cannot connect to server "); <br>
  mysql_select_db("$db_name")or die("cannot select DB")</p>
<p>$sql="SELECT * FROM $tbl_name";<br>
  $result=mysql_query($sql);</p>
<p>$rows=mysql_fetch_array($result);<br>
  $counter=$rows['counter'];</p>
<p>// if have no counter value set counter = 1<br>
  if(empty($counter)){<br>
  $counter=1;<br>
  $sql1="INSERT INTO $tbl_name(counter) VALUES('$counter')";<br>
  $result1=mysql_query($sql1);<br>
  }</p>
<p>echo "You 're visitors No. ";<br>
  echo $counter;</p>
<p>// count more value<br>
  $addcounter=$counter+1;<br>
  $sql2="update $tbl_name set counter='$addcounter'";<br>
  $result2=mysql_query($sql2);</p>
<p>mysql_close();<br>
  ?></p>



, , ,


Hello there! If you are new here, you might want to subscribe to the RSS feed for updates on this topic, or follow us on Twitter. Get promotion with Hypesol.
Name: Email:


Subscribe

Subscribe to our e-mail newsletter to receive updates.

One Response to “PHP Counter Script Tutorial”

  1. Best PHP Training Says:

    THANKS for information .

    Reply

Leave a Reply

Get Adobe Flash player