« Drupal SEO |
Create a Site Authentication Login with PHP and MySQL »
Displaying Click Counts with PHP and MySQL
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
[...] 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. [...]
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!
[...] 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 [...]
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.
[...] via Displaying Click Counts with PHP and MySQL | Studge. [...]
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
The demo is linked to at the bottom of the copy – here.