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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:56:24+00:00 2026-05-31T11:56:24+00:00

Why are we closing the $result $mysqli = new mysqli(localhost, root, root, test); if

  • 0

Why are we closing the $result

    $mysqli = new mysqli("localhost", "root", "root", "test");
    if ($mysqli->connect_errno) {
        echo "Failed to connect to MySQL: " . $mysqli->connect_error;
    }

    if ($result = $mysqli->query("Select * from user")) {
        while ($row = $result->fetch_object())
        {
            //var_dump($row);
        }
        $result->close();
    }   
  • 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-05-31T11:56:25+00:00Added an answer on May 31, 2026 at 11:56 am

    Your check if the connection failed falls through to the code that uses the connection. Obviously that won’t work because it’s not a live connection. Make sure that if the connection fails, code that depends on it is not reached. (Note that I’m not necessary advocating use of exit or die. They can make for a rather bad user experience. They can be useful, but ideally you should output a better message and leave the ugly error stuff for logs [unless you’re only testing or it’s a command line script]).

    As for close() (which is actually an alias for free()):

    free() basically deallocates all of the stuff attached to result. You need to understand that there are two (simplification, but go with it) ways of retrieving rows:

    Buffered – Everything is snatched in one go. In other words, by the time you start looping through the records, they’re all actually already in memory. They are buffered. They are not being pulled from the server every time you call fetch() but rather are being pulled from memory.

    Non buffered – there may be a small buffer, but essentially every time you call fetch(), PHP must pull a new row from the database. At the beginning of the loop, they are not all sitting in memory.

    Interesting note: num_rows() can be called on a buffered query very efficiently since all of the rows are already pulled (though you should never pull all of the rows just to count them — that’s what COUNT is for). Non buffered queries cannot do num_rows() until they pull all of the rows. Meaning it will either be an error, or will essentially turn it into a buffered query.

    Anyway, back to your actual question:

    free() frees anything associated with the result object. In the case of a buffered query, this is any rows sitting in memory. In the case of a non buffered query, free() will release whatever rows may be sitting in memory and then cancel the rest of the request. Basically its PHP telling MySQL, “Hey you know all those rows I request? Changed my mind. You can just drop that request.”

    As for if you should free results… Well, whenever the variable goes out of scope*, it will happen anyway. There is, however, no harm in explicitly doing it, and in situations where a query may use a lot of memory and then another query after it may use a lot of memory, you may wish to free the sets just to keep memory usage low.

    * Or maybe when the request ends. I don’t remember off the top of my head.

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

Sidebar

Related Questions

Using the new MySQL Query Profiler in PHPMyAdmin version 2.11.5, I got following results
I have a while loop that I use to echo multiple mysql results that
I'm getting my xml file as a result of a php query from some
I'm getting an unexpected result for my query in group function. I'm using the
Edit: closing anchor fixed. This issue exists when testing on the following browsers: Google
I'm closing a WCF endpoint in the page_unload of an asp.net page, when using
I'm closing in on finishing a Windows Desktop Gadget that downloads plugins from my
Hi all (before closing based on the title, please read the bottom parragraph), I'm
What are all the valid self-closing elements (e.g. <br/>) in XHTML (as implemented by
W3C recommends putting a space before the closing tag in XHTML , because this

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.