Trying to get a characters stats with equipment added.
However I don't know how to add up the numbers from a column.
This is what I have, and it just echo's each equipments speed bonus, it doesn't add them.
$stats= "SELECT * FROM `playerarmor` WHERE `cid`='$cid' AND `equip`='1'";
if ($result1 = mysql_query($stats)){
while ($row2 = mysql_fetch_row($result1)) {
echo $row2[9];
}}
Adding Up SQL Columns[solved]
Adding Up SQL Columns[solved]
Last edited by MikeD on Sun Oct 23, 2011 9:20 pm, edited 1 time in total.
- Jackolantern
- Posts: 10891
- Joined: Wed Jul 01, 2009 11:00 pm
Re: Adding Up SQL Columns
You can do this right in the query! Here is a tutorial explaining how 
The indelible lord of tl;dr
Re: Adding Up SQL Columns
Thanks, got it right.
However, without doing 10 different queries, how would I change this so I could get the result of 10 columns?
However, without doing 10 different queries, how would I change this so I could get the result of 10 columns?
Code: Select all
$query = "SELECT speedadd, SUM(speedadd) FROM `playerarmor` WHERE `cid`='$cid' AND `equip`='1'";
$result = mysql_query($query) or die(mysql_error());
// Print out result
while($row = mysql_fetch_array($result)){
echo $row['SUM(speedadd)'];
}- Jackolantern
- Posts: 10891
- Joined: Wed Jul 01, 2009 11:00 pm
Re: Adding Up SQL Columns
Well, I know you can sum multiple columns without additional requirements like this:
But I am not exactly sure how you could add the additional sum requirements onto that pattern (like the $cid and equipped requirements). You may have to use subqueries.
Code: Select all
SELECT sum(`speed`) + sum(`speedbonus`) FROM `players`The indelible lord of tl;dr
Re: Adding Up SQL Columns[solved]
hint: remove ALL trailing quotes for your queries, only use single or double quotes ( depending on your PHP string ) for strings in the query. IE: ints, doubles, floats, datetimes et cetera should not carry trailing back or trailing qoutes. Neither should your table name or fields. Why? Because debugging goes a lot faster and it forces you to use good naming conventions for your tables.