loony383 Posted August 18, 2008 Share Posted August 18, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/120273-solved-counting-up-values-from-an-database/ Share on other sites More sharing options...
Fadion Posted August 18, 2008 Share Posted August 18, 2008 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/120273-solved-counting-up-values-from-an-database/#findComment-619619 Share on other sites More sharing options...
loony383 Posted August 19, 2008 Author Share Posted August 19, 2008 I dont want to count the number of rows I need the total value of all the values from an collum so say I had a 1 collum table wtht the values 5 4 5 when added up I would get 14 however counding therows would give me 3. Quote Link to comment https://forums.phpfreaks.com/topic/120273-solved-counting-up-values-from-an-database/#findComment-619921 Share on other sites More sharing options...
Fadion Posted August 19, 2008 Share Posted August 19, 2008 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']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/120273-solved-counting-up-values-from-an-database/#findComment-619923 Share on other sites More sharing options...
loony383 Posted August 19, 2008 Author Share Posted August 19, 2008 Awesome thank you so much for the help. Quote Link to comment https://forums.phpfreaks.com/topic/120273-solved-counting-up-values-from-an-database/#findComment-619928 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.