Old 03-28-2009   #1 (permalink)
Registered User
 
Space Cowboy's Avatar
 

Join Date: May 2005
Location: England (South)
Posts: 43

Sum of collum then echo value

PHP Code:
mysql_connect(localhost,$username,$password);
   @
mysql_select_db($database) or die( "Unable to select database");
   
$total mysql_query("SELECT SUM(amount) FROM printing");
   echo 
'Total Size: '.$total.''
The above code prints out the following

Total Size: Resource id #5

SELECT SUM(amount) FROM printing; produces a numerical value when i run the query through terminal.

Can anyone help Please

-Trev
__________________
Space Cowboy

Thanks for all the help here guys!
Space Cowboy is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 03-28-2009   #2 (permalink)
Janitor of Lunacy
 
Tamlin's Avatar
 

Join Date: May 2006
Location: Sitting in the Wishing Chair
Posts: 5,758
Blog Entries: 2
Images: 530

Battle Wins: 20 (rank: #1)
As I've mentioned before, I don't really know much about PHP and MySQL (my experience is with ASP.NET and T-SQL, which seems considerably more logical), but I did find the following, which might put you on the right track (if someone more knowledgeable doesn't happen by):

Quote:
The mysql_query function returns a resource. If you print a resource
you get the text "Resource id #n" where n is replaced with its ID. To
make use of this resource you need to use functions like
mysql_fetch_array, mysql_fetch_assoc or one of the many others
detailed in the manual.
PHP: MySQL - Manual

Since you are using an aggregate function (SUM), I'm guessing that the result set will consist of one row and one column, so (if I understood the manual correctly) you should be able to use the mysql_fetch_row function to get the value you require, and then display it as follows:
PHP Code:
$row mysql_fetch_row($total);

echo 
$row[0]; 
Alternatively, this might work:
PHP Code:
echo mysql_result($total0); 
__________________

"I might join your century, but only as a doubtful guest"

Last edited by Tamlin; 03-28-2009 at 05:29 PM.
Tamlin is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 04-07-2009   #3 (permalink)
Registered User
 

Join Date: Apr 2009
Posts: 12

Do you want to retrieve a the sum of a column and output it?
Code:
$sqla= "SELECT *,SUM(column) AS Total FROM Table1 INNER JOIN Table2 
ON Table1.oneid = Table2.oneid WHERE Table1.coid= '$co' AND MONTH='$month' && YEAR= '$year' "; 

$result = mysql_query($sqla) 
or die (mysql_error()); 
if ($myrow = mysql_fetch_array($result)) { 

   do { 
$id=$myrow["Nameid"]; 
$total=$myrow["Total"]; // $total will be set to the sum column's value

// More code here, end loop.

echo 'The total is ' .  $total; // You could use $myrow['Total'] as the variable.
?>
The column is a sum column, so only needs to be set once...the query actually only needs to be run once if you want the sum. Otherwise you could just keep a running total of a column without MySQL with $total .= $myrow['Total'];
harish is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hello *echo* .psd Introductions 9 03-20-2007 04:31 AM


All times are GMT +1. The time now is 01:51 AM.

Powered by vBulletin Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.3.2

Design & Content © BioRUST 2008 :: PRIVACY STATEMENT :: LEGAL INFORMATION :: ADVERTISING MEDIA KIT