Craig Hopson Posted September 1, 2012 Share Posted September 1, 2012 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 More sharing options...
niche Posted September 1, 2012 Share Posted September 1, 2012 (edited) 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 September 1, 2012 by niche Link to comment Share on other sites More sharing options...
JamesB Posted September 1, 2012 Share Posted September 1, 2012 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 1 Link to comment Share on other sites More sharing options...
Craig Hopson Posted September 1, 2012 Author Share Posted September 1, 2012 So what would I echo then? Link to comment Share on other sites More sharing options...
JamesB Posted September 1, 2012 Share Posted September 1, 2012 $row['malai'] The string given to $row should match the one used after 'AS' in the query. Link to comment Share on other sites More sharing options...
Craig Hopson Posted September 1, 2012 Author Share Posted September 1, 2012 $row[`malai`] don't work Link to comment Share on other sites More sharing options...
Craig Hopson Posted September 1, 2012 Author Share Posted September 1, 2012 (edited) 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 September 1, 2012 by Craig Hopson Link to comment Share on other sites More sharing options...
JamesB Posted September 1, 2012 Share Posted September 1, 2012 $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]; 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now