Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 514
  • Last Modified:

Calling & Storing PHP Values

Hi, i am working on a site that allows members to make a small donations to a user's (friend) marathon profile page… i have a 'marathon_profile' table in my database and i have a 'donations' table in my database.

- id
- user_id
- total_amount
+ 10 more columns

- id
- user_id
- marathon_id
- amount
+ 5 more columns

I need to display the total amount raised on the marathon profile and i am trying to decide on how i should store / record the total amount raised and how i should query the total amount in order to display it:

01 - which table should i store the total amount of money received in
A - simply add / increment the $total_amount field in the 'marathon_profile' by the individual donation amount AND record each individual donation $amount in the 'donations' table
B - JUST record the individual amount in the 'donations' table and forget the idea of having a $total_amount and incrementing it each time

02 - how should i call for the total amount so that i can display it on the marathon profile page
A - just call for the $total_amount in the 'marathon_profile' table (this seems a lot less workload on the site)
B - run a query which will add up add the $amounts WHERE marathon_id = the marathon page (this seems a lot more work load on the site)

Thanks in advance for your help
3 Solutions
No total, if you need to get the total value then

assuming MySQL

Rereading your question - you have multiple users/marathons?
The query would just need adapting for the id of the person/marathon - not sure which you would be selecting by...?
Question 01 ...
I would go with B.  Do not store (and maintain) data that can be derived easily.
Question 02 ...
Again B.  A 'SELECT SUM(amount) FROM Donations WHERE marathon_id = ?' is workload on the database server ... and unless the table get's very large - not that much work ... it is what they are designed to do.  Having appropriate indexes may help keep the workload down too.

That is my opinion ... let's see what others say.
oo7mlAuthor Commented:
Ok, thanks all... just thought that that approach would slow the site down as it would need to run that query each time a user visits the marathon page
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Well everytime they visit the page you still have to run a query to get the details so makes no difference.
oo7mlAuthor Commented:
Good point... thanks for the advice guys...
Ray PaseurCommented:
No points for this, please, but regarding this...
slow the site down as it would need to run that query each time a user visits
Consider the number of rows that your tables are likely to contain.  If there were ten thousand marathon runners gathering donations through the site, that is nothing at all in terms of the load on a modern server.  Just follow the common-sense guidance for dealing with data bases -- use good indexes, avoid SELECT *, don't store anything like BLOBs, use EXPLAIN SELECT on any query that joins tables, etc.  You'll be fine!

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now