Results 1 to 10 of 10

Hybrid View

  1. #1
    Join Date
    Jun 2012
    Posts
    481
    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

  2. #2
    Join Date
    Jan 2004
    Location
    N of San Antonio TX
    Posts
    9,684
    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?
    A little help with colors.
    myZenCartHost.com - Zen Cart Certified, PCI Compatible Hosting by JEANDRET
    Free SSL & Domain with semi-annual and longer hosting. Updating 1.5.2 and Up.

  3. #3
    Join Date
    Jul 2012
    Posts
    16,816
    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...

  4. #4
    Join Date
    Jul 2012
    Posts
    16,816
    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...

  5. #5
    Join Date
    Jun 2012
    Posts
    481
    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

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