Jump to content
Larry Ullman's Book Forums

Clarification On When To Use "Mysqli_Close($Dbc);" And Mysql_Free_Result()

Recommended Posts

I've never been clear about when these two functions need to be called and (if they are never called) when $dbc is closed automatically and when the result of a query is "freed" automatically.


My example is this:


I have a php page that has a form. The first thing that is done is to fill it in with default data from the database via a SELECT. From here, the data can be edited and then saved.


1. Should I "mysqli_close($dbc);" AFTER the "Select" query and BEFORE the form is validated and then "UPDATED" or is it okay to leave it alone and just close it after the form has been "UPDATED"? And what about "free_Result". Should this always be done along with the "close" function or does it only matter according to how mach data is being read from the DB?


2. Is the database closed and the result freed when a different .php page is accessed?


I've never run into trouble doing or not doing these but I think I'd like to know the SOP.



Link to post
Share on other sites

I guess freeing the results is for when you want to conserve as many (server) resources as possible. Although, with the exception of big applications that need to be optimized, I can't imagine any difference being seen between freeing resources versus not freeing resources.


For closing the DB connection, I wouldn't recommend closing it between the SELECT and UPDATE queries. That seems like a waste.

However, aside from saving resources, I imagine there are security reasons for closing the MySQLi connection as soon as your done with it, which is why it makes sense (to me) to close the connection after the UPDATE query.


As for when the DB connection closes on its own, hmmm...I really don't know. If I had to guess, I would say it closes when the page closes, but I don't know.

  • Upvote 1
Link to post
Share on other sites

Non persistant database connections are closed automatically at the end of script execution.


If you're pre-populating a form with data from a database, and a user submits that form, these are 2 script executions and therefore 2 separate connections to the database.


You could close the connection manually after both your SELECT and UPDATE queries - I've assumed the form posts data back to the same script and the UPDATE query won't be called unless the script tests true for form submission.


mysqli_free_result frees up memory used by the result of your query.


General rule is use mysqli_free_result after executing queries and mysqli_close when you know you no longer require a database connection for the rest of the current script execution.


I think it's good practice to use both, can also make code easier to review for you and others.

Link to post
Share on other sites

I tend to leave the connection open after the SELECT figuring there will be an UPDATE soon to follow. The query usually involves very little data so I wasn't sure if I was being anal in "closing" and "freeing" all the time. Also I didn't know if there was any kind of security risk involved. But if the connection is closed automatically, as you say, at the end of script execution, then what is the point in ever "closing" it via script (i.e. what constitutes a persistent connection). The DB is opened, the form populated, then the DB is closed until the UPDATE comes along. Then it is closed automatically after the UPDATE. Do I understand this correctly?


Non persistent database connections are closed automatically at the end of script execution.


If you're pre-populating a form with data from a database, and a user submits that form, these are 2 script executions and therefore 2 separate connections to the database.


Link to post
Share on other sites

Rob and HartleySan have been on point, but to add my own two cents...


The short answer is that PHP and MySQL will perform automatic clean up, including closing the database connection when a script terminates and freeing up query resources. The two arguments for doing either of these yourself are:

  1. Not to rely upon assumed behavior and to be overt.
  2. To tidy up those resources sooner than PHP would automatically.

I try to remember to do both for reason #1, but sometimes forget. On particularly involved scripts, I'll do both because of reason #2.

Link to post
Share on other sites

I'm from the 'good ol days' of computing when "out of MEM error" wasn't uncommon on my Apple (orTRS-80) computer screen. So when I hear about "freeing up memory" I shudder a bit of what might happen if I don't do it religiously.


Anyway, thanks to all the posts I have enough understanding of how it works to make my own decisions about it.


thanks as always

Link to post
Share on other sites
  • 1 year later...

Hi everyone. 


I'm new to the forum. Just about finished the book.  Fantastic read and thus far, has been one of the best learning experiences of my life.

Many thanks Larry.


One question I have relates to this topic.


Perhaps someone could give a helpful analogy to better explain ( to me ) the meaning behind 'saving server resources'  re: mysqli_free_result();   


Specifically, if a particular php script has several separate database queries,   if one uses this function appropriately after each query, are the results of each query useable at the end of the script, or are the results lost when you free the result?


I know I could test the scripts to see how they behave, but armed with a bit of knowledge prior to testing would save time and potential frustration.


Many thanks in advance.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...