++ 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...
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;
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.....
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;
Re: ++ is increasing variable by 2
Quote:
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")
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.