This was a typo question and I didn’t really want to leave my code up here. It would not benefit anyone else.
This is my code and I want the variable $x to be itself plus the value of $points. For some reason, this is the output I get:
TOTAL Before Add: 0
points: 8
TOTAL after add: 8
TOTAL Before Add: 0
points: 32
TOTAL after add: 32
I want this to make x: 40 (adding 8+32).
Why is x starting over at 0 again every time?
Appreciate the help,
R
In the code, you confuse
$row3and$row4, but I’ll assume you’re iterating over one row here. Also, you seem to present a heavily abstracted version here. Verify that the bug appears even after the simplification, for example with a test database. But assuming that’s not the case, the only explanation is:You’re executing the whole code snippet multiple times. Each time it is executed, it reads exactly one line. Add
before the while and you’ll see two runs.
As a final note, you should really only use PDO in post-2010 code. It is database-independent and has excellent support for transactions and prepared statements.
In response to the edit:
First of all, your code is vulnerable to SQL injection. Fix that now, for example by using PDO prepared statements.
Secondly, why are you joining in php code? You should calculate the sums and all the data you need on the database, with multiple JOINs. You should not need more than one(maybe two) SQL query for that.
Thirdly, the problem is that you’re not adding to $total:
Since
$Totalis undefined, it’s evaluated as0, so you could equally write:Of course, you wanted:
But as I mentioned above, it’s still an extremely bad coding style to perform trivial calculations (such as summing up) in the application instead of the database.