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 9175067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:51:16+00:00 2026-06-17T16:51:16+00:00

I made a database which collects information on a daily basis. as like calender

  • 0

I made a database which collects information on a daily basis. as like calender the database stores members’ daily amount. If month doesn’t match any existing month(M-Y) the database will create a new html month table. I have solved this problem, as follows:

mysql_query("something goes here");
while(condition)
{
    mysql_query("something goes here")
    while(condition)
    {
        mysql_query("something goes here");
        while()
        {
            ....................
        }
        ........................
    }
}

This algorithm worked well when I discovered it. However, after a few days, it was placing a heavy load on my server. I then tried the same algorithm in PHP (but I can’t this). How can I make this run faster?
The code is as follows:

        $q2=mysql_query("SELECT  a.member_id,a.dates,MONTH(dates) AS months,
                        YEAR(dates)AS years,sum(amount) as sums
                        FROM account AS a
                        left join member as m
                        on(a.member_id=m.member_id)
                        GROUP BY (SELECT EXTRACT(YEAR_MONTH FROM dates))
                        ORDER by dates DESC
        ");
        $k=0;
        while($data2=mysql_fetch_assoc($q2))
        {
            $months=$data2['months'];
            $years=$data2['years'];
            $daten = new DateTime($data2['dates']);

            print "<tr><th align='left'><b>".$daten->format('F-Y')."</b></th>";

            $q3=mysql_query("select * from member");

            while($data3=mysql_fetch_assoc($q3))
            {
                $id3=$data3['member_id'];
                $q4=mysql_query("
                                SELECT SUM(amount) AS total FROM account
                                WHERE member_id=$id3
                                AND month(dates)=$months
                                AND year(dates)=$years                              
                            ");
                while($data4=mysql_fetch_assoc($q4))
                {
                    $total=$data4['total'];

                    print "<td class='total'>".number_format($total)."</td>";
                }
            }
            print "<td class='total'><b>".$data2['sums']."</b></td></tr>";
            $k=$k+$data2['sums'];
        }
  • 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-17T16:51:17+00:00Added an answer on June 17, 2026 at 4:51 pm

    Among other things:

    • You’re running the query SELECT * FROM member for every row in the first query. This query is independent of the loop, so running it again every time is wasteful.

    • For each result from the SELECT * FROM member query, you’re running another query (SELECT SUM(amount) AS total FROM account ...). There are several issues with this query:

      • First of all, this query could be combined into the previous query using a GROUP BY, to avoid having to run one query for every member. Something like:

        SELECT member_id, SUM(amount) AS total FROM account WHERE ... GROUP BY member_id

      • Second of all, you’re using MONTH(dates) = $months AND YEAR(dates) = $years. This is inefficient, as it forces the server to examine every row; converting it to a range on dates (e.g, dates BETWEEN '$year-$months-01' AND '$year-$months-31') would speed things up if there were an appropriate index on dates.

    In general: Avoid queries in loops. The number of queries involved in generating a page should, to the degree possible, always be a small, nearly constant number. It should not grow with your data.

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

Sidebar

Related Questions

I have made a dump of my database in which one table is pretty
i have a windows application made in VB.NET which maintains a database, and i
I've made POCO object mapped to database like this: class MoneyObject { Money MoneyAmmount
I have built a simple application which connects database made in derby(Netbeans) which binds
OK. I have database which is made from data used in TRAVIAN game. I
I have a program that collects information from the database. in my previous question
I have made an json api for a database which has some products, and
I have a PHP MySQL database which I will be storing all my information
We do database releases which are made up of a group of change sets
I've made a database using phpMyAdmin , now I want to make a register

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.