Jump to content

Video Counter dependant on number of plays


MarlonM

Recommended Posts

I am creating a website where various users can upload videos as well as view them. Below is what I have done so far but it updates all the video with the same count and updates the DB with the ID and hits but fails to update the videoid. Anyone have any ideas. 

 

 <?php

 

$getViews = mysql_query("SELECT `hits` FROM `views` WHERE `id` = $video_id");
if(mysql_num_rows($getViews)) {
  $result = mysql_fetch_assoc($getViews);
  $videoViews = $result['hits']+1; 
} else {
  $videoViews = 1;
}
 
 
mysql_query("INSERT INTO `views` (`id`) VALUES ($video_id) ON DUPLICATE KEY UPDATE hits = hits+1");
 
echo 'Total Views: '.number_format($videoViews);
 
?>
 
My DB is called views and the columns within are ID, hits and videoid. 
 

Are you using upper or lower case for id?

From what I see you only need 2 columns ID and hits, since you seem to be trying to use the $video_id variable as id. If ID is an autoincrement would not work as you expected.

 

ditch the autoincrement on ID if have it and drop the videoid column

$getViews = mysql_query("SELECT `hits` FROM `views` WHERE `ID` = $video_id");

if(mysql_num_rows($getViews)) {

  $result = mysql_fetch_assoc($getViews);
  $videoViews = $result['hits']+1; 
} else {
  $videoViews = 1;
}



mysql_query("INSERT INTO `views` (ID,hits) VALUES ('{$video_id}','1')
ON DUPLICATE KEY UPDATE hits = hits+1");



echo 'Total Views: '.number_format($videoViews);

Was another post I did about tracking clicks or views plus by dates.

Note it's using mysqli_ functions instead of the deprecated mysql_

 

http://forums.phpfreaks.com/topic/296442-unique-click-tracking-using-php/?do=findComment&comment=1512504

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.