Jump to content

Add up total SUM()


Craig Hopson

Recommended Posts

hi all cant get this to work where am i going wrong?

$result = mysql_query("SELECT SUM(malai) FROM orders WHERE delivery = 0");while($row = mysql_fetch_array($result))  {echo $row['malai'];   }

i get Resource id #7 from this what i want is to add the total from colum and then echo it

Link to comment
Share on other sites

According to http://www.w3schools...ql_func_sum.asp you'll get is a single number so you probably won't have to put your fetch array in a while loop. Echo a var_dump() after your fetch array to see how to get access to your number. Also, always put

or die(mysql_error());

after your queries for more complete error reporting.

Edited by niche
Link to comment
Share on other sites

Also if you're looking to access the total number via a string into $row, instead of an integer into $row, you should use the 'as' keyword in the query:http://www.w3schools.com/sql/sql_alias.asp eg.

SELECT SUM(malai) AS malai FROM orders WHERE delivery = 0

  • Like 1
Link to comment
Share on other sites

how about thisSOLVED

$result = mysql_query("SELECT SUM(malai) FROM orders WHERE delivered = 0");while($row=mysql_fetch_array($result)){echo $row['SUM(malai)'];}

but instead of while loop how else could this be done?

Edited by Craig Hopson
Link to comment
Share on other sites

$result = mysql_query('SELECT SUM(malai) AS total FROM orders WHERE delivered = 0');$row = mysql_fetch_assoc($result);echo $row['total'];

$result = mysql_query('SELECT SUM(malai) FROM orders WHERE delivered = 0');$row = mysql_fetch_row($result);echo $row[0];

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...