Click to See Complete Forum and Search --> : ++ is increasing variable by 2


rhboarder
October 1st, 2010, 01:03 PM
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:

$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...

PeejAvery
October 2nd, 2010, 06:46 AM
Echo $count before you increment it and after. You will see that it does not add 2. The operand itself cannot do that.

$count = $data['hitcount'];
echo $count;
$count++;
echo $count;

rhboarder
October 2nd, 2010, 01:56 PM
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.....

PeejAvery
October 4th, 2010, 11:19 AM
Copy and paste the output of the following code for us...please use quote tags.

$data = mysql_fetch_assoc($res);
print_r($data);
$count = $data['hitcount'];
echo $count;
$count++;
echo $count;
$query = "UPDATE hitcount SET hitcount='".$count."'";
echo $query;

rhboarder
October 4th, 2010, 05:41 PM
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")

PeejAvery
October 5th, 2010, 02:52 PM
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.