Jump to content

[SOLVED] counting up values from an database


loony383

Recommended Posts

Hi, I'm trying to make an script and in the script I need to have the total of an collum in my mysql table but not for an specefic amount of results I need it to be able to handle unlimited amount of table rows. I have no idea how to do this so if someone can hive a few tips I would be greatful. This has to be done in php/mysql as later on in my script I will need toe total value.

Simply:

 

<?php
$results = mysql_query("SELECT col FROM table");
echo mysql_num_rows($results); //it will print the total amount of rows the query returns
?>

 

or just by adding COUNT in the query:

<?php
$results = mysql_query("SELECT COUNT(col) as totalRows FROM table");
$values = mysql_fetch_array($results);
echo $values['totalRows']; //will return the total amount of rows
?>

Ohh I didn't understand it as your explanation wasn't very clear (at least to me). You can use SUM in this case, which will calculate the sum of a column in all rows.

 

<?php
$results = mysql_query("SELECT SUM(col) as totalSum FROM table");
$values = mysql_fetch_array($results);
echo $values['totalSum'];
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.