Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8322829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:30:54+00:00 2026-06-08T23:30:54+00:00

I have a table on my php page populated with data from a MySQL

  • 0

I have a table on my php page populated with data from a MySQL query from a single table

The table looks like:

Location    Jan Feb Mar etc…    Total
Location1   5   13  7       25
Location2   10  10  6       26  
Location3   22  1   7       29
Etc…

The ‘Total’ is calculated by the query using Sum ie SUM(IF(month = '1', 1,0)) AS 'jan', SUM(IF(month = '2', 1,0)) ASfeb, SUM(IF(month = '3', 1,0))

What I want are the totals for each column (Jan, Feb, Mar etc)

I can do this in PHP by adding the values as they are extracted but can I do it the query – and is there an advantage to doing so?

Thanks in advance for help rendered.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T23:30:59+00:00Added an answer on June 8, 2026 at 11:30 pm

    To me, this is a strange layout of a database, but I normally use a row based approach along these lines:

    location   | month | total |
    loc1       |   1   |  3    |
    loc2       |   1   |  4    |
    loc1       |   2   |  7    |
    etc...
    

    which makes totals and the like MUCH easier to perform.

    However, given you structure, you could either continue to do what you are doing or use a little trickery. If you are doing more work with it in PHP (aside from just the totals) you could easily do a concat() in SQL and get a preformatted string that is ready to be explode()-ed into an array. From there you can use all the wonderful array functions in PHP to get totals or anything else you like.

    Edit: Righto, that makes some difference.

    If you are using a normal row based approach to storing the data, you can probably make the database do all the work. If you need a query to get the individual months AND the totals, you can either do it in PHP of course, but you can also do a little trick like this:

    select
        month, // assuming 1-12 for example
        sum(someData)
    from
        yourTable
    where
        someCondition=1
    group by
        month
    union all
    select
        13 as month, // assuming 1-12 for example
        sum(someData)
    from
        yourTable
    where
        someCondition=1
    group by
        month
    

    This is handy if you have a DB that has loads of free CPU, but a webserver that is struggling or you have a very LARGE amount of data that needs to be tallied up for example. Just make the database do the crunching and return it as some specific number you can identify in your PHP code – months 1-12 will be normal, month 13 is the total.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When showing data from a mysql table on an php page does it have
I have a working mysql query that retrieves data from table1. Now I will
I have a PHP page with a mysql connection, a select query and then
I have a table populated with data from the database, where each row has
I have a table FOLDERS ,everytime a folder is added through a php page
I have web page in PHP which displays all records in a table. I
I have a table generated by PHP/mysql, I want a user to click the
i work with php/Mysql i have table with two cols (date , cash) with
I have created a php table like so: echo <table border = '0' cellpadding
I am using MYSQL and PHP. I have a table called item. Two of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.