Calling & Storing PHP Values

Posted on 2012-09-05
Last Modified: 2012-10-06
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
Question by:oo7ml
    LVL 58

    Accepted Solution

    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...?
    LVL 25

    Assisted Solution

    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.

    Author Comment

    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
    LVL 58

    Expert Comment

    Well everytime they visit the page you still have to run a query to get the details so makes no difference.

    Author Comment

    Good point... thanks for the advice guys...
    LVL 107

    Assisted Solution

    by:Ray Paseur
    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!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Suggested Solutions

    Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
    This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
    This video teaches users how to migrate an existing Wordpress website to a new domain.
    The viewer will learn how to count occurrences of each item in an array.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now