Jump to content

unique visits per 24hr


subhomoy
Go to solution Solved by Barand,

Recommended Posts

Hiii every one i'm trying to count the impression of the specific image once in 24hr.

 

here is the code

<?php
require_once '../dbconfig.php';
$mid = $_REQUEST['id'];      <-------------------------   Image Id
 $ip = $_SERVER['HTTP_HOST'];<------------------------- Viewers ip
 $date = date("Y-m-d");<-------------------------  current date
$qh = mysql_query("SELECT date,ip FROM image_hits WHERE image_id='$mid'") or die(mysql_error()); <------------------------- It will check whether any rows using that image id is present or not... if not it will produce "0"
 while($a = mysql_fetch_array($qh)){
     $date1 = $a['date'];
     $ip1 = $a['ip'];
 }
 if(($ip==$ip1) && ($date==$date1)){<------------------------- Checking current date with the database values
     $h = mysql_query("SELECT visits FROM image_hits WHERE image_id='$mid'") or die(mysql_error());
     while($b = mysql_fetch_array($h)){
         $visits = $b['visits'];
     }
     echo $visits;
     }
 else {    <----------------------- it will select the current values and will increment it by 1 and ten insert into database...
    $qh1 = mysql_query("SELECT visits FROM image_hits") or die(mysql_error());
    while($c = mysql_fetch_array($qh1)){
    $visits1 = $c['visits']; 
    }
    $visits2 = $visits1+1;
   $hits = mysql_query("UPDATE image_hits SET visits='$visits2' WHERE date='$date' AND ip='$ip'") or die(mysql_error());      
   while($d = mysql_fetch_array($hits)){<-------------------------the error shows...    
       $visits = $d['visits'];
   }
   echo $visits1;
   }
?>

The warning message is hsown below

 

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\Mobitalk.com\members\image_hits.php on line 25

 

Any help will greatly appreciated....

 

If there is any easier way to do then plz recommend...

 

Thank You....

 

Link to comment
Share on other sites

Your issue is because you're using inline variables improperly.  you're looking for records that have a date value of '$date' rather than the value contained therein. if you want to see what i mean, just add this in place of line 23

die("UPDATE image_hits SET visits='$visits2' WHERE date='$date' AND ip='$ip'");

That's the string you're passing to mysql

***edit*** also, you should be checking mysql_affected_rows() to get the number affected for an update, rather than fetching results.

Edited by seandisanti
Link to comment
Share on other sites

  • Solution

So this is what you are trying to do?

 

 

 

else {    <----------------------- it will select the current values and will increment it by 1 and ten insert into database...
    $qh1 = mysql_query("SELECT visits FROM image_hits") or die(mysql_error());
    while($c = mysql_fetch_array($qh1)){
    $visits1 = $c['visits'];
    }
    $visits2 = $visits1+1;


Loop through all records in image_hits table, get the number of visits from the last record then increment it.

Then

 

$hits = mysql_query("UPDATE image_hits SET visits='$visits2' WHERE date='$date' AND ip='$ip'") or die(mysql_error());


Update all the images visited by that IP today and give them all the same number of visits
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.