|
-
October 1st, 2010, 01:03 PM
#1
++ 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...
-
October 2nd, 2010, 06:46 AM
#2
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.
-
October 2nd, 2010, 01:56 PM
#3
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.....
-
October 4th, 2010, 11:19 AM
#4
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.
-
October 4th, 2010, 05:41 PM
#5
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
-
October 5th, 2010, 02:52 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|