Page 1 of 1

Mysql Count?

Posted: Sat Mar 25, 2006 7:34 am
by Qaid
Hi Everyone,

I'm just coding some stuff in PHP and MySQL.

I have multiple rows with a row called "total", inside total theres a number (can be different to every post)
In my php file i want to make a mysql output that takes only from the row "total" and count it all together.

Example: Inside the row "total" i have two post. The first with 15 and the second with 3.
Then it should post in my php file: 18.

I tried using this code:

Code: Select all

<? $query = mysql_query("SELECT * from total"); echo mysql_num_rows($query); ?>
But this count the rows?

Frustrated :(

Thanks for the help guys...

Posted: Sat Mar 25, 2006 7:40 am
by s.dot
Try this:

Code: Select all

$result = mysql_query("SELECT SUM(`total`) FROM `table`");
$total =  mysql_result($result,0);
echo $total;
or

Code: Select all

$result = mysql_query("SELECT `total` FROM `table`");

$total = 0;
while($array = mysql_fetch_assoc($result)){
   $total += $array['total'];
}
echo $total;