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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:03:34+00:00 2026-06-06T14:03:34+00:00

I have a script that is taking too long to run and I have

  • 0

I have a script that is taking too long to run and I have been working on optimizing it to be quicker. One thing I know is taking extra time is the fact that i have to run the same query multiple times. I’d like to be able to store the result of that query (without writing it to a db) so that I can reference it’s value later without having to run the query again.

I am dramatically simplifying the script below but hopefully this gives you an idea.

Let’s say i have two functions that each return a value:

    function a(){
          $query = "SELECT SUM(price) FROM table_1 WHERE zip='$this->zip'"; 
          $result = mysql_query($query) or die(mysql_error());
          while($row = mysql_fetch_array($result)){
          return $row['SUM(price)'];
    }
    function b(){
          $query = "SELECT SUM(price) FROM table_2 WHERE zip='$this->zip'"; 
          $result = mysql_query($query) or die(mysql_error());
          while($row = mysql_fetch_array($result)){
          return $row['SUM(price)'];
    }

Let’s suppose function a() returns a value of 10 and function b() returns a value of 5

Now, suppose my script is something like this:

    if (function a() > 5) { echo 'it is greater than 5!' }
    if (function a() < 5) { echo 'it is less than 5!' }
    if (function b() == 5) { echo 'it equals 5!' }
    if (function b() > 10) { echo 'it is not greater than 5!' }

If i understand things properly, the script above causes 4 separate queries of my database. It is possible to store the initial running of each query? One thing i thought of was doing this:

    $a = function a();
    $b = function b();

And then having my script like this:

    if ($a > 5) { echo 'it is greater than 5!' }
    if ($a < 5) { echo 'it is less than 5!' }
    if ($b == 5) { echo 'it equals 5!' }
    if ($b > 10) { echo 'it is not greater than 5!' }

Will that cause the queries to only run once each at the point when i define $a and $b? Is there another method to consider that i am not thinking of?

Thanks for your help!

  • 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-06T14:03:35+00:00Added an answer on June 6, 2026 at 2:03 pm

    Yes, this will cause your queries to only run once. If you want to ensure that they run once per session without manually caching the result in a variable every time, you could look into an ORM with some caching ability, or use static variables inside each function:

    function a() {
      static $cached_result;
      if (!is_null($cached_result) return $cached_result;
    
      $query = "SELECT SUM(price) FROM table_2 WHERE zip='$this->zip'"; 
      $result = mysql_query($query) or die(mysql_error());
      while($row = mysql_fetch_array($result)){
        return $cached_result = $row['SUM(price)'];
      }
    }
    

    If you don’t want to use an ORM, you should at the very least be writing your own slim series of wrapper functions around database access, rather than manually calling mysql_query in every function. There should really be only one point where your app touches the database through the mysql_* family of functions. This isolates change; had you implemented a small wrapper around your database access in the first place, you would now only have one place to change in order to ditch the dated mysql_query functions in favour of PDO.

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

Sidebar

Related Questions

I have this script that run to fix my menu bar to the browser
I have script that reads remote file content and writes it to local server.
I have a script that recursively loops through all the sub directories and compresses
I have a script that submits data via an ajax POST. It is called
I have following script that executes all the .reg files in the current directory.
I have a script that obtains information about the current folders and subfolders within
I have a script that resizes called resize Here is the script: <? echo
I have a script that uses a ton of redirects. About half way through
I have a script that reads a list of addresses from a table and
I am currently working on a problem that I have an inelegant solution for.

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.