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.

Link to comment
Share on other sites

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
?>

Link to comment
Share on other sites

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'];
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.