Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Oct 2003
    Location
    Australia
    Posts
    112
    Thanks
    0
    Thanked 0 Times in 0 Posts

    A bug on counting

    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

    Code:
    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

  • #2
    Regular Coder
    Join Date
    Sep 2002
    Location
    British Columbia
    Posts
    235
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #3
    Regular Coder
    Join Date
    Oct 2003
    Location
    Australia
    Posts
    112
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    New Coder
    Join Date
    Sep 2003
    Posts
    98
    Thanks
    0
    Thanked 0 Times in 0 Posts
    $counter++;

    or

    $counter = $counter + 1;
    or

    $counter = ($counter + 1);

    ++; is shorter and better

  • #5
    Regular Coder
    Join Date
    Sep 2002
    Location
    British Columbia
    Posts
    235
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Why the while loop? Would "select * from templates where tid='$tid'" ever return more than one row?

  • #6
    Regular Coder
    Join Date
    Oct 2003
    Location
    Australia
    Posts
    112
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.. I'll try the $counter++ . thank you.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •