Current location: Hot Scripts Forums » Programming Languages » PHP » Store and display link click counts to/from MySQL Database?


Store and display link click counts to/from MySQL Database?

Reply
  #1 (permalink)  
Old 05-16-04, 04:06 AM
rottedham rottedham is offline
Newbie Coder
 
Join Date: May 2004
Location: Katy, TX
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Question Store and display link click counts to/from MySQL Database?

Hi,

I'm working on a download script and I was wondering if anyone could tell me how to store the amount of times a file has been downloaded to a database. I have an understanding of how to display it, I just don't know what to do to add up the hits. If anyone can help me, I'd greatly appreciate it.

Thanks.
Reply With Quote
  #2 (permalink)  
Old 05-16-04, 09:47 AM
Klesti Klesti is offline
Newbie Coder
 
Join Date: May 2004
Location: Albania
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by rottedham
Hi,

I'm working on a download script and I was wondering if anyone could tell me how to store the amount of times a file has been downloaded to a database. I have an understanding of how to display it, I just don't know what to do to add up the hits. If anyone can help me, I'd greatly appreciate it.

Thanks.
Yor mysql table have to contain a field named hits
for example :
id
url
author
hits

in a file called download.php you will executate 2 mysql queries. i supposed that your mysql table is called downloads
PHP Code:

$sql "SELECT * from downloads WHERE id=$id"//Here $id is the id taken from the url f.e download.php?id=23

$result mysql_query($sql);
$row mysql_fetch_assoc($result);
$hits $row['hits'];  //here is the number of hits 
//now increase the number of hits 
$hits $hits 1;
//lets upadate it
$sql "UPADATE downloads SET hits="$hits" WHERE id=$id"
mysql_query($sql); 
Reply With Quote
  #3 (permalink)  
Old 05-16-04, 10:03 AM
rottedham rottedham is offline
Newbie Coder
 
Join Date: May 2004
Location: Katy, TX
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
thank you SOO much. I've been trying to figure this out all night, just messing around with the code and you've told me exactly what i needed to know.
Reply With Quote
  #4 (permalink)  
Old 05-16-04, 10:34 AM
rottedham rottedham is offline
Newbie Coder
 
Join Date: May 2004
Location: Katy, TX
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
I am having an issue running this script. I am getting a warning saying "mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource."
Reply With Quote
  #5 (permalink)  
Old 05-16-04, 03:11 PM
dgm dgm is offline
New Member
 
Join Date: May 2004
Location: Simi Valley, CA.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Or, you could do it in one query.

PHP Code:

$sql "UPDATE downloads SET hits = hits+1 where id=$id"
That would be much more efficient.

Last edited by dgm; 05-16-04 at 03:14 PM.
Reply With Quote
  #6 (permalink)  
Old 05-16-04, 04:23 PM
Klesti Klesti is offline
Newbie Coder
 
Join Date: May 2004
Location: Albania
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Maybe you didn't pass the $id variable.

for understanding if the result is taken :
PHP Code:

 $sql "SELECT * from downloads WHERE id=$id"//Here $id is the id taken from the url f.e download.php?id=23 

if (!$result mysql_query($sql)) {

echo 
"Error: " mysql_error();
}

$row mysql_fetch_assoc($result); 
$hits $row['hits'];  //here is the number of hits 
//now increase the number of hits 
$hits $hits 1
//lets upadate it 
$sql "UPADATE downloads SET hits="$hits" WHERE id=$id"
if (!
mysql_query($sql)) {
echo 
"error" mysql_error();

You should be secured that the variable $id is passed to the script because the sql query won't work without it and will show you that error

Last edited by Klesti; 05-16-04 at 04:27 PM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump


All times are GMT -5. The time now is 10:34 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.