On Thu, 7 Feb 2002 21:07:44 -0600, you wrote:
>At 21:28 -0500 2/7/02, Floyd Baker wrote:
>>On Thu, 7 Feb 2002 19:54:51 -0600, you wrote:
>>
>>>At 19:53 -0500 2/7/02, Floyd Baker wrote:
>>>>I have a column of amounts that was getting adding in a loop but I see
>>>>there's a sum() function to do this. Should have known. But I can't
>>>>make it work. Can someone give me a simple query syntax using php?
>>>
>>>The SQL SUM() function has nothing to do with PHP.
>>>
>>>SELECT SUM(some_numeric_column) FROM tbl_name
>>>
>>>is one example of how it works.
>>>
>>
>>
>>That I know. I have your book in my hand... But I'm not able to make
>>it work from a php script. By php syntax I mean something like your
>>example on page 360 using count(). I'm replacing it with sum().
>>
>>
>>$result = mysql_query( "select SUM('cost') from $table");
>>if($row = mysql_fetch_array($result)) print "RESULT = $row(0)";
>>
>>I get result = array(0)... Can you tell me what's wrong?
>
>Ah.
>
>You're asking SUM() to sum the value of the string 'cost' for each
>row of the result set. Assuming that cost is the name of the column
>you want to sum, replace SUM('cost') with SUM(cost).
>
>>
>>Thanks much.
>>
>>Floyd
>
Well that made sense but unfortunately it didn't work. I see that
adding strings isn't too good but it's still coming up with array(0).
I have another query ahead and everything works. The connection,
table, etc. are ok.
$query = "select * from $table";
$mysql_result = mysql_query($query, $mysql_link);
while($row = mysql_fetch_array($mysql_result))
{
$cost = $row['cost'];
print "<br>COST = $cost<br>"; //Prints each record's cost.
}
$result = mysql_query( "select SUM(cost) from $table");
or exit();
if($row = mysql_fetch_array($result)) print "RESULT = $row(0)";
I've been trying lots of arrangements but nothings happening.
Floyd
--
---------------------------------------------------------------------
Before posting, please check:
http://www.mysql.com/manual.php (the manual)
http://lists.mysql.com/ (the list archive)
To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php