I have a simple php/mysql script that stores in mysql database id, link, and date fields. It stores various links and some of them appear several times. I would like to count the number of times each link was accessed. I was thinking of adding new field counter and updating it every time new record is inserted, but this needs to check if the link already exists and update counter when necessary.
This means I would have to:
1. Search the entire table to look if the link already inserted.
2. If no – insert it and set counter to value of 1.
3. If yes – find the correct record and update its counter by 1.
I would like to have something like:
1. http://www.link1.com 11:20
2. http://www.link2.com 11:30
3. http://www.link1.com 11:40
http://www.link1.com counter: 2 times
http://www.link2.com counter: 1 time
I am wondering what is the best way to do that? Should I rather create a new table that stores the counter for each link?
It depends on what you need to track. Do you only need to track total number of visits? If so, add a column to your table. You don’t need to check wheter the link exists if you want to update the counter this way.
If you need to track who visisted when etc, create a new table with a foreign key and neccesary columns to your table. With this, you need to have the id of your link, but when you update the number of visits, you should already have it.