Mysql Count?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Qaid
Forum Commoner
Posts: 33
Joined: Wed Feb 15, 2006 12:04 pm

Mysql Count?

Post 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...
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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;
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply