subhomoy Posted April 25, 2013 Share Posted April 25, 2013 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.... Quote Link to comment Share on other sites More sharing options...
seandisanti Posted April 25, 2013 Share Posted April 25, 2013 (edited) 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 April 25, 2013 by seandisanti Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted April 25, 2013 Solution Share Posted April 25, 2013 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.