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

  • Home
  • SEARCH
  • 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 8302007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:10:43+00:00 2026-06-08T17:10:43+00:00

Ever since developing my first MySQL project about 7 years ago, I’ve been using

  • 0

Ever since developing my first MySQL project about 7 years ago, I’ve been using the same set of simple functions for accessing the database (although, have recently put these into a Database class).

As the projects I develop have become more complex, there are many more records in the database and, as a result, greater likelihood of memory issues.

I’m getting the PHP error Allowed memory size of 67108864 bytes exhausted when looping through a MySQL result set and was wondering whether there was a better way to achieve the flexibility I have without the high memory usage.

My function looks like this:

function get_resultset($query) { 
    $resultset = array();

    if (!($result = mysql_unbuffered_query($query))) {
        $men = mysql_errno();
        $mem = mysql_error();
        echo ('<h4>' . $query . ' ' . $men . ' ' . $mem . '</h4>');
        exit;
    } else {
        $xx = 0 ;
        while ( $row = mysql_fetch_array ($result) )  {
            $resultset[$xx] = $row;
            $xx++ ;
        }
        mysql_free_result($result);
        return $resultset;
    }
}

I can then write a query and use the function to get all results, e.g.

$query = 'SELECT * FROM `members`';
$resultset = get_resultset($query);

I can then loop through the $resultset and display the results, e.g.

$total_results = count($resultset);

for($i=0;$i<$total_results;$i++) {
    $record = $resultset[$i];

    $firstname = $record['firstname'];
    $lastname = $record['lastname'];

    // etc, etc display in a table, or whatever
}

Is there a better way of looping through results while still having access to each record’s properties for displaying the result list?

I’ve been searching around for people having similar issues and the answers given don’t seem to suit my situation or are a little vague.

  • 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-08T17:10:46+00:00Added an answer on June 8, 2026 at 5:10 pm

    Your problem is that you’re creating an array and filling it up with all the results in your result set, then returning this huge array from the function. I suppose that the reason for which this is not supported by any mysql_* function is that it’s extremely inefficient.

    You should not fill up the array with everything you get. You should step through the results, just like you do when filling up the array, but instead of filling anything, you should process the result and get to the next one, so that the memory for this one gets a chance to be freed.

    If you use the mysql_* or mysqli_* functions, you should return the resource, then step through it right there where you’re using it, the same way you’re stepping through it to fill the array. If you use PDO, then you can return the PDOStatement and use PDOStatement::fetch() to step through it.

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

Sidebar

Related Questions

Ever since JQuery came along a few years ago I've been using it in
Ever since I created this Rails 3.2.3 project to use MiniTest, I have been
Ever since I discovered sockets, I've been using the nonblocking variants, since I didn't
Ever since I started programming this has been something I have been curious about.
Ever since I moved from VB6 to VB.NET somewhere in 2005, I've been using
Ever since the first beta came out I’ve been trying to find out if
This has been bugging me ever since I started learning Ruby five months ago:
Been running into a problem ever since I started using rvm to manage my
Well I have been thinking about this for a while, ever since I was
Ever since the publication of Enterprise Integration Patterns people have been using the notation

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.