Results 1 to 10 of 10
  1. #1
    Join Date
    Jul 2012
    Posts
    16,732
    Plugin Contributions
    17

    Default reuse of SQL query results without rerunning query and with minimal memory use

    I am working on a mod to a plugin. Currently results of a query are used to present all of the applicable information on the screen with a summary at the end; however, I would like to be able to provide the summary at the beginning.

    The two methods of this I can conceive are to save the results of the query to a second variable, step through as needed to get the summary, and then use the second variable to resent the information and repeat the results. Ibelieve this would use extra memory unnecessarily.

    The second option I can think of is to run the query, calculate, run the query and display; however, this has the potential of providing two different results. (The second containing more info than the first.)

    Isn't there some type of command to return back to the start of the query result or to capture a pointer to the head and reset the query back to the identified position?
    ZC Installation/Maintenance Support <- Site
    Contribution for contributions welcome...

  2. #2
    Join Date
    Sep 2009
    Location
    Stuart, FL
    Posts
    12,478
    Plugin Contributions
    88

    Default Re: reuse of SQL query results without rerunning query and with minimal memory use

    You can use a code construct similar to:
    Code:
    $result = $db->Execute($your_sql_code);
    while(!$result->EOF) {
      <perform your summary calculations>
      $result->MoveNext();
    }
    $result->Move(0);  // Move to first element ...
    $result->MoveNext();  // Required to not process first element twice
    while(!$result->EOF) {
      <do your line-by-line display>
      $result->MoveNext();
    }
    unset($result);

  3. #3
    Join Date
    Jul 2012
    Posts
    16,732
    Plugin Contributions
    17

    Default Re: reuse of SQL query results without rerunning query and with minimal memory use

    A little unclear on the use of movenext immediately after move(0). Wouldn't I need to enter the second loop with the pointer at the first element? (Assuming that the first element is at position zero.) That way if zero elements are returned then the loop would be bypassed like the first loop would be? Or does position zero point to BOF and there is no info in it and if there were no elements returned that position "1" would be EOF?

    Btw, sorry, THANK YOU for your prompt response and what looks like a functional and simple solution.
    ZC Installation/Maintenance Support <- Site
    Contribution for contributions welcome...

  4. #4
    Join Date
    Sep 2009
    Location
    Stuart, FL
    Posts
    12,478
    Plugin Contributions
    88

    Default Re: reuse of SQL query results without rerunning query and with minimal memory use

    Quote Originally Posted by mc12345678 View Post
    A little unclear on the use of movenext immediately after move(0). Wouldn't I need to enter the second loop with the pointer at the first element? (Assuming that the first element is at position zero.) That way if zero elements are returned then the loop would be bypassed like the first loop would be? Or does position zero point to BOF and there is no info in it and if there were no elements returned that position "1" would be EOF?

    Btw, sorry, THANK YOU for your prompt response and what looks like a functional and simple solution.
    The requirement for the MoveNext() call after the Move(0) call is based on the core code's operation. Essentially, the Move(0) kinda-sorta-not-exactly positions the pointer and the call to MoveNext() finalizes the operation. I'm want to call it a "bug" since there are at least two plugins (one of them mine) that "assume" this behavior.

  5. #5
    Join Date
    Jul 2012
    Posts
    16,732
    Plugin Contributions
    17

    Default Re: reuse of SQL query results without rerunning query and with minimal memory use

    Quote Originally Posted by lat9 View Post
    The requirement for the MoveNext() call after the Move(0) call is based on the core code's operation. Essentially, the Move(0) kinda-sorta-not-exactly positions the pointer and the call to MoveNext() finalizes the operation. I'm want to call it a "bug" since there are at least two plugins (one of them mine) that "assume" this behavior.
    I'm good with that. (Ie haven't actually "arrived" at the beginning until the first movenext after a move(0). ) If that's what php/mySQL needs, then that's what it will get. :) again, thank you.
    Last edited by mc12345678; 17 Oct 2013 at 10:57 PM.
    ZC Installation/Maintenance Support <- Site
    Contribution for contributions welcome...

  6. #6
    Join Date
    Jun 2012
    Posts
    412
    Plugin Contributions
    0

    Default Re: reuse of SQL query results without rerunning query and with minimal memory use

    I ran into a similar problem troubleshooting a modification to a plugin. I used a foreach after a query to output some variables in the query results. Subsequent plugin code used a while (!$results->EOF) loop to process the query results again, but consistently fails. I discovered that !$results->EOF was false at the while loop even though there were results from the preceding foreach loop. I tried LAT9's approach but at least EOF is not restored. Removing my troubleshooting foreach loop eliminated the problem and the while loop executed normally.

    I would like to understand and avoid this problem in future troubleshooting activities. Any other suggestions on how to reset the query results including EOF?

    TIA. Dave
    zc157c, PHP 8.0.22, mysql 5.7.39

  7. #7
    Join Date
    Jan 2004
    Location
    N of San Antonio TX
    Posts
    9,145
    Plugin Contributions
    11

    Default Re: reuse of SQL query results without rerunning query and with minimal memory use

    Quote Originally Posted by Dave224 View Post
    zc157c, PHP 8.0.22, mysql 5.7.39
    While 1.5.7c should not have problems with PHP 8.0, your version of the mod may not like that version.
    Have you tried dropping the PHP down a notch?

  8. #8
    Join Date
    Jul 2012
    Posts
    16,732
    Plugin Contributions
    17

    Default Re: reuse of SQL query results without rerunning query and with minimal memory use

    Quote Originally Posted by Dave224 View Post
    I ran into a similar problem troubleshooting a modification to a plugin. I used a foreach after a query to output some variables in the query results. Subsequent plugin code used a while (!$results->EOF) loop to process the query results again, but consistently fails. I discovered that !$results->EOF was false at the while loop even though there were results from the preceding foreach loop. I tried LAT9's approach but at least EOF is not restored. Removing my troubleshooting foreach loop eliminated the problem and the while loop executed normally.

    I would like to understand and avoid this problem in future troubleshooting activities. Any other suggestions on how to reset the query results including EOF?

    TIA. Dave
    zc157c, PHP 8.0.22, mysql 5.7.39
    Seeing that you are using a version greater than the base Zen Cart 1.5.5, there is a database method called rewind that will accomplish your desired goal.

    If you did a query that resulted in the variable $results containing your query results, then calling: $results->rewind(); at a point after your usage and before the plugin's while loop, then the query results should be reset to the beginning and allow "proper" processing.

    Alternatively, the plugin could be updated to also use the foreach loop, though be mindful that the internal loop may have done some other actions to the query loop that need to be considered. "Normal" code though would be to change the while to a foreach and other changes likely identified: https://docs.zen-cart.com/dev/code/d...-row-responses

    Mind you that wasn't exactly easy to find, but it is an example of differences.
    ZC Installation/Maintenance Support <- Site
    Contribution for contributions welcome...

  9. #9
    Join Date
    Jul 2012
    Posts
    16,732
    Plugin Contributions
    17

    Default Re: reuse of SQL query results without rerunning query and with minimal memory use

    What I did in a plugin that still bridges Zen Cart versions was to test for the existence of the method, if it existed then do the rewind, if it didn't then use the ->Move(0) operation then do a ->MoveNext() operation.

    Code:
     if (method_exists($results, 'rewind')) {
     $results->rewind();
    } else {
     $results->Move(0);
     $results->MoveNext();
    }
    With that being any time after initially touching $results, but before the next desired access of the original $results.
    ZC Installation/Maintenance Support <- Site
    Contribution for contributions welcome...

  10. #10
    Join Date
    Jun 2012
    Posts
    412
    Plugin Contributions
    0

    Default Re: reuse of SQL query results without rerunning query and with minimal memory use

    Thank you so much mc. $results->rewind() did the trick! Donation on the way.
    Dave

 

 

Similar Threads

  1. v154 Help with a SQL Query for Query Builder
    By lindasdd in forum Managing Customers and Orders
    Replies: 2
    Last Post: 24 Mar 2016, 01:18 PM
  2. v139h Use of SQL Query Executor
    By GerryTheMole in forum General Questions
    Replies: 5
    Last Post: 26 Mar 2014, 03:07 PM
  3. v151 SQL query setup. How do I TEST a query 'off-line'?
    By lewisasding in forum General Questions
    Replies: 3
    Last Post: 8 Mar 2013, 12:24 AM
  4. Help with a sql query
    By batteryman in forum General Questions
    Replies: 21
    Last Post: 3 Oct 2008, 11:12 PM
  5. Formatting SQL query results
    By haasPlay in forum Contribution-Writing Guidelines
    Replies: 1
    Last Post: 1 Aug 2008, 09:37 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
disjunctive-egg
Zen-Cart, Internet Selling Services, Klamath Falls, OR