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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:54:08+00:00 2026-05-13T22:54:08+00:00

I have some questions about using MySQLi queries, and related memory management. Suppose I

  • 0

I have some questions about using MySQLi queries, and related memory management.

Suppose I have something like this:

$db = new mysqli($dbhost, $un, $ps, $dbname);

$query = "SELECT field1, field2  FROM table1 ";
$results = $db->query($query);

while ($result = $results->fetch_object()) {
    // Do something with the results
}

$query = "SELECT field1, field2 FROM table2 ";
// question 1
$results = $db->query($query);

while ($result = $results->fetch_object()) {
    // Do something with the second set of results
}

// Tidy up, question 2
if ($results) {
    $results->free();
}
if ($db) {
    $db->close();
}

// Question 3, a general one

So, based on the comments in the code above, here are my questions:

  1. When I assign the results of the second query to $results, what happens to the memory associated with the previous results? Should I be freeing that result before assigning the new one?

  2. Related to 1, when I do clean up at the end, is cleaning up just the last results enough?

  3. When I do try to clean up a result, should I be freeing it as above, should I be closing it, or both?

I ask question 3 because the PHP documentation for mysqli::query has an example that uses close, even though close is not part of mysqli_result (see example 1 in the link above). And in contrast, my normal PHP reference text uses free (PHP and MySQL Web Development, Fourth Edition, Welling and Thomson).

  • 1 1 Answer
  • 2 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-05-13T22:54:08+00:00Added an answer on May 13, 2026 at 10:54 pm

    When I assign the results of the
    second query to $results, what happens
    to the memory associated with the
    previous results?

    When you execute this:

    $results = $db->query($query);
    

    If there was something in $results before, this old content cannot be accessed anymore, as there is no reference left to it.

    In such a case, PHP will mark the old content of the variable as "not needed anymore" — and it will be removed from memory when PHP needs some memory.

    This, at least, is true for general PHP variables; in the case of results from an SQL query, though, some data may be kept in memory on the driver-level — over which PHP doesn’t have much control.

    Should I be freeing that result before
    assigning the new one?

    I never do that.

    Related to 1, when I do clean up at
    the end, is cleaning up just the last
    results enough?

    When the scripts end:

    • The connection to the database will be closed — which means any memory that might be used by the driver should be freed
    • All variables used by the PHP script will be destroyed — which means the memory they were using should be freed.

    So, at the end of the script, there is really no need to free the result set.

    When I do try to clean up a result,
    should I be freeing it as above,
    should I be closing it, or both?

    If you close the connection to the database (using mysqli::close like you proposed), this will disconnect you from the database.

    This means you’ll have to re-connect if you want to do another query! Which is not good at all (takes some time, resources, … )

    Generally speaking, I would not close the connection to the database until I am really sure that I won’t need it anymore — which means I would not disconnect before the end of the script.

    And as "end of the script" means "the connection will be closed" even if you don’t specify it; I almost never close the connection myself.

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

Sidebar

Related Questions

I am using Tomcat 6 and have some questions about Apache mod_jk as follows.
I am currently using Visual Studio Express C++ 2008, and have some questions about
I have some questions about the performance of this simple python script: import sys,
Hi I have two questions about MySQL and QUERIES. First of all i've like
I have some questions about importing data from Excel/CSV File into SQL Server. Let
i have some questions about constructors in ColdFusion : must i use the name
I have some questions about the default values in a function parameter list Is
I have some questions about vector in STL to clarify..... Where are the objects
I have some questions about the registry. We have Preferences p = Preferences.userRoot(); If
I have created a wildcard App ID and have some questions about bundle ID

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.