Clicks counter (php)

Let´s learn how to make a simple clicks counter in php. In this tutorial we will need MySQL.

So...Let´s start:

creat in your database a table named 'clickcounter' with the properties like the code below.
[syntax="php"]// CREATE TABLE clickcounter (
// id int(11) NOT NULL auto_increment,
// url varchar(50) NOT NULL default '',
// count smallint(3) NOT NULL default '',
// PRIMARY KEY (id),
//) TYPE=MyISAM;[/php]
Ok, first you will need to make the connection to your database. Is what this first line does. If u don´t know how to make the connection to your goes:
$localhost = "localhost";
$dbuser = "your_db_name";
$dbpass = "your_db_pass";
$dbname = "your_db_name";

mysql_connect($localhost, $dbuser, $dbpass) or die
@mysql_select_db ("$dbname") or die(mysql_error());

Let´s call the connection file
include "connection_file.php";[/syntax]
This will increment the count field value of the requested id.
[syntax="php"]$update = mysql_query("UPDATE clickcounter SET count = count + 1 WHERE id='$id'");[/syntax]
Select the requested id from the database..
[syntax="php"]$result = mysql_query("SELECT url FROM clickcounter WHERE id='$id'");[/syntax]
Retrieve the data(url) of url field.
[syntax="php"]$row = mysql_fetch_array($result);
$url = $row[url];[/syntax]
Redirect to the retrieved url and close the database.
[syntax="php"]header("Location: $url");

That´s all. Now save this file as (e.g: click.php) and call this script making something like this:
click.php?id=10 :wink:

Nice, i like this tutorial - just the way it should have been done too. Easy to read and well structured. We all want to measure click counts - so well done, and thank you gesf!

You can view more of his php tutorials at:

I found some small errors on the "click count" script above so i fixed it, and also highlighted the php syntax ;)