TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-11-2007, 02:09 PM   #1 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default MySQL Query; Counting and adding values

I myself find this a little bit disturbing. It sounds so easy, so practical and so common to use, but I can't seem to grasp it. Not even FIND OUT how it works. Thats why this kinda belongs in the "Absolute Beginners" board. :P

I want to add values, for example I've got a table layout:

PHP Code:
    id--    itemname--    itemcosts--
    
1        item1            1
    2        item2            7
    3        item3            2 
Adding the values itemcosts in a for/foreach/while loop will result in the integer 10. The point is, I want this done WITHOUT a loop of any kind. I want the mySQL Query to do the work for me. I've conciddered the COUNT option but else it's all a blank.

So, in 1 query, lets say $fCount, I want to get the integer 10.

Can anyone help me out here? :)
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 12-11-2007, 03:00 PM   #2 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

If you are displaying the data, you have to use a loop. Just add the costs while you are displaying them and print them after the loop.

If you want it from just a query,
PHP Code:
SELECT SUM(itemcosts) AS fCount FROM table WHERE 1
That will add all the values of field and assign it to total. Now just use this to print it

PHP Code:
$query mysql_query("SELECT SUM(itemcosts) AS fCount FROM table WHERE 1;");
$result mysql_fetch_array($query);
$fCount $result["fCount"]; 
__________________


Last edited by Village Idiot : 12-11-2007 at 04:03 PM.
Village Idiot is offline  
Reply With Quote
The Following User Says Thank You to Village Idiot For This Useful Post:
SOCK (12-11-2007)
Old 12-11-2007, 03:18 PM   #3 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

Wow, that simple huh. A mate of mine knew that there was a simple word for it but that ... I myself was thinking about ADDING numbers, which forms SUM a couple of numbers. Simply couldn't think of the word.

Thanks Village.

Lock? :)
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 12-11-2007, 10:07 PM   #4 (permalink)
The Acquainted
 
Join Date: Nov 2007
Posts: 154
Thanks: 31
SOCK is on a distinguished road
Default

FYI - there is a huge index of MySQL functions available in the online MySQL Manual.
SOCK is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 08:15 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design