Re: Count database-values

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I screwed up my own code.  Silly me.  It should read:

$query = "SELECT SUM(bedrag) AS Total FROM finance WHERE posneg = 'af'";
$result = mysql_query( $query );
while ( $total = mysql_fetch_array( $result ) )
{
    $total = $total['Total'];
}

Edward Dudlik
Becoming Digital
www.becomingdigital.com


----- Original Message ----- 
From: "Becoming Digital" <subs@becomingdigital.com>
To: <php-db@lists.php.net>
Sent: Tuesday, 10 June, 2003 06:43
Subject: Re:  Count database-values


> So far no problems, but I want to count all these values.
> I tried with SUM but with the following code it doesn't work:
>
> $squery = "SELECT SUM(bedrag) AS Total FROM finance WHERE posneg = 'af'";
>
> any suggestions how to do? I want to store it in a variabele ($total)
> so I can echo it. (E.g.: $total = 93.63 in this case)

If you want the total, you need to do something like the following:

$query = "SELECT SUM(bedrag) AS Total FROM finance WHERE posneg = 'af'";
$result = mysql_query( $query );
$total = mysql_fetch_array( $result );
$total = $total['Total'];

You had forgotten to pull the data out of your result set.  It's a common
mistake. :)

Edward Dudlik
Becoming Digital
www.becomingdigital.com


----- Original Message -----
From: "Robert Wanadoo" <robertvandermast@wanadoo.nl>
To: <php-db@lists.php.net>
Sent: Monday, 09 June, 2003 11:40
Subject:  Count database-values


Hi everybody,

I've got a little problem, which I can't solve. It is as follow:
In my database I made a table with different fields.
One of the fields is named 'bedrag' and contains a numeric value like 15.47 or
78.16 and so on.
If I want a value of the table I use in most cases the following code:

<?
$squery = mysql_query("SELECT * FROM finance WHERE posneg = 'af'",$db);
while($row = mysql_fetch_array($squery)) {

$bedrag = $row["bedrag"];
echo "$bedrag<br>";
}
?>

In this case I receive a list like:
15.47
78.16
and so on...

So far no problems, but I want to count all these values. I tried with SUM but
with the following code it doesn't work:

$squery = "SELECT SUM(bedrag) AS Total FROM finance WHERE posneg = 'af'";

any suggestions how to do? I want to store it in a variabele ($total) so I can
echo it. (E.g.: $total = 93.63 in this case)

Thanx in advance,

Robert van der Mast




-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php





-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux