...

View Full Version : A bug on counting



celestine
11-11-2003, 03:48 AM
I've been trying to write a counter to count the number of times a link is click. Unfortunately, PHP is acting really weird for me.

Currently running PHP 4.3.3



include_once('xxx/yyy.php'); // my function, mysql connection file

$qAvatar = "select * from templates where tid='$tid' ";
$rsAvatar = db_query($qAvatar);

while ($row = mysql_fetch_array($rsAvatar))
{
extract($row);

$downfile ="$pspfile";
$pspdowncount+=1;
$up = "update templates set pspdowncount='$pspdowncount' ";

$up .= " where tid='$tid' ";
$rsUp = db_query($up);

if ($rsUp)
{
echo "$up";
echo "<p class=header2>Download Templates</p>";
echo "<p align=center><a href=$downfile>Download $mode version here</a></p>";

}


}


What happens is that it increment sometimes by 1, sometimes by 2. Seems to be random but mostly increment by 2. Is this a PHP bug or that I have calculated wrongly ? Thanks

Dylan Leblanc
11-11-2003, 06:55 AM
You can put the increment directly in the query:

UPDATE templates SET pspdowncount = pspdowncount + 1 WHERE tid = '$tid'

Also, I don't see why you need the while loop.

celestine
11-11-2003, 06:58 AM
thanks I wrote my query this way originally (ie directly incrementing in the query) but I encountered the same bug.

Also, when I used Mozilla to do the incrementing, (by loading the page in Mozilla), it works, no problem. When I used IE 6, however, I encounter the random bug.

Acecool
11-11-2003, 06:03 PM
$counter++;

or

$counter = $counter + 1;
or

$counter = ($counter + 1);

++; is shorter and better

Dylan Leblanc
11-11-2003, 06:08 PM
Why the while loop? Would "select * from templates where tid='$tid'" ever return more than one row?

celestine
11-12-2003, 03:12 AM
Originally posted by Dylan Leblanc
Why the while loop? Would "select * from templates where tid='$tid'" ever return more than one row?

Ahh right.. Its a bad habit of mine.. :p I'll try the $counter++ . thank you.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum