« | »

Displaying Click Counts with PHP and MySQL

By Studge | May 19, 2007

Terry at SuperAff.com has asked about a follow up to a previous post Hiding Links With PHP and Counting Clicks with MySQL. In this article I demonstrated how to hide links (affiliate or otherwise) and count how many times each was clicked. Here I will show how to throw together a simple MySQL query to show you the click count.

First, you need to follow the aforementioned post to set up the click counter. In the interest of not repeating myself, I will omit that code from this post. Following is the PHP code that we will use:

<?php
  mysql_connect("mysql.example.com", "username", "password");
  @mysql_select_db("database") or die( "Unable to connect to database");
  $result = mysql_query("SELECT * FROM linkcount");

  $num = mysql_num_rows($result);

  mysql_close();

  echo "<table><tr><td>Link</td><td>Count</td></tr>";

  for ($i=0; $i<$num; $i++) {
	  if ($tmp = mysql_fetch_array($result)) {
		  extract($tmp);

		  echo "<tr><td>$id</td>";
		  echo "<td>$count</td></tr>";
	  }
  }

  echo "</table>";
?>

There are many different ways to code a front-end like this. I am by no means a PHP expert, but I know enough to usually get the job done. This code will give you a good starting point and you can elaborate on the code with some simple HTML to dress up the display table a bit. I would usually advise against using tables in your web development ventures, but when it comes to returning MySQL queries there really isn't a better way.

To display these results in a Wordpress post, you will need a plugin to allow running custom PHP code. Wordpress does not allow the execution of PHP code by default. In this blog I only show the code and do not have a real need to embed it – so I do not currently use one of these plugins. However, you can view the results of the above code here.

Topics: (X)HTML, MySQL, PHP, Web Development

Share: del.icio.us | digg | reddit

RSS feed | Trackback URI

7 Comments »

2007-05-19 12:08:14

[...] Edit: A follow-up has been written to this post that describes a simple script for displaying the click results: Displaying Click Counts with PHP and MySQL. [...]

 
Comment by Terry
2007-05-19 17:20:23

Fantastic! This is exactly what I was hoping to my hands on :) , works like a dream.

Thanks very much, this is a great piece of code!

 
2007-05-19 17:23:52

[...] through with some more code–wooohooo! You’ll find the code to view the stats here: Displaying Click Counts with PHP and MySQL. Works GREAT, a big thanks to [...]

 
Comment by Wayne
2008-02-25 16:59:21

Can you help me out with some code to get me started with implementing a way to track these links via date also. I.e. how many times the link was accessed per date.

 
2009-09-15 20:55:16

[...] via Displaying Click Counts with PHP and MySQL | Studge. [...]

 
Comment by yrahs
2010-08-27 01:14:56

hi can you give a demo using the script?
so newbies like me will be able to use it efficiently..
i wanted to put beside my download link how many times it was clicked..
hope to hear from you :)

Comment by Studge
2010-08-27 08:25:54

The demo is linked to at the bottom of the copy – here.

 
 
Name (required)
E-mail (required - never shown publicly)
URI
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> in your comment.