CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2009
    Location
    California
    Posts
    31

    ++ is increasing variable by 2

    This is probably the simplest thing but I'm not catching it, basically im increasing a variable that got its value from a database by 1 (so var++) but then I check it and it had been increased by 2. heres the code:
    Code:
    $res = mysql_query($query);
    //retrieve hitcount from the returned array
    $data = mysql_fetch_assoc($res);
    $count = $data['hitcount'];
    //$count++;
    mysql_free_result($res);
    //enter the new value of hitcount to the database
    $query = "UPDATE hitcount SET hitcount='".$count."'";
    mysql_query($query);
    After its run, if i go into the database to and view the value of hitcount its 1 more than it should be...

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: ++ is increasing variable by 2

    Echo $count before you increment it and after. You will see that it does not add 2. The operand itself cannot do that.

    PHP Code:
    $count $data['hitcount'];
    echo 
    $count;
    $count++;
    echo 
    $count
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Nov 2009
    Location
    California
    Posts
    31

    Re: ++ is increasing variable by 2

    i did do that, as in printing the variable before and after, but something I dont know if its something to do with how its being entered into the database but everytime the code executes and i go into the database to check the value it had been increased by 2.....

  4. #4
    Join Date
    May 2002
    Posts
    10,943

    Re: ++ is increasing variable by 2

    Copy and paste the output of the following code for us...please use quote tags.

    PHP Code:
    $data mysql_fetch_assoc($res);
    print_r($data);
    $count $data['hitcount'];
    echo 
    $count;
    $count++;
    echo 
    $count;
    $query "UPDATE hitcount SET hitcount='".$count."'";
    echo 
    $query
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  5. #5
    Join Date
    Nov 2009
    Location
    California
    Posts
    31

    Re: ++ is increasing variable by 2

    Array
    (
    [hitcount] => 0
    )
    0
    1
    UPDATE hitcount SET hitcount='1'
    (I tried using the quote tag but when I'd try to preview the post it said an error "the me[quote]ssage you entered is too short")
    Last edited by PeejAvery; October 5th, 2010 at 02:52 PM. Reason: Added quote tags

  6. #6
    Join Date
    May 2002
    Posts
    10,943

    Re: ++ is increasing variable by 2

    That's because you need more than just a quote in your post. It's okay...I added them.

    Now, that means that the code responsible for the incorrect hitcount is not what you have posted. It's somewhere else.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured