title: When should I close a database connection in PHP?
body: <p>I'm nothing like a php developer but I do have to use it and I'm not really aware on how PHP handle memory allocation during session.   </p>
  
  <p>I'm working on an application that ask for an HTTP authentication, once you're logged in you can manipulate data through a nice interface.<br>
  Someone told me on another post, that I shouldn't close the mysql data connection after every execution, but I don't know how this connection stay is memory during using this app because in the server side I have no idea what PHP keeps in memory or not.  </p>
  
  <p>So my question is should I use a singleton to connect to the db and never close it (because I will never know when the application is not in use. Or should I stand with what I do today: opening connection --> execute statement --> close connection.</p>
  
  <p>PS: I'm using mysqli</p>
  
  <p><strong>Edit 1</strong>:<br>
  My application is design with MVC pattern meaning :  </p>
  
  <pre><code>|''''''''''|      |'''''''''''''|      |''''''''''''''|
  | view.php |  ==&gt; | control.php |  ==&gt; | database.php |
  |----------|      |_____________|      |______________|
  </code></pre>
  
  <p>That pattern allow the view to interact with data only through the <code>control.php</code> which then call a function from database.php to <code>SELECT</code> or <code>EDIT</code> data. with the explanation you give me, I should put:   </p>
  
  <pre><code>public function __destruct(){
      mysql_close($this-&gt;connection);
  }
  </code></pre>
  
  <p>inside <code>database.php</code>, but that page is load when there's a need to select or modify data, so it's only executed for a short time, meaning it will still close the connection at the end of the request.  </p>
  
  <p>Which gives a more precise question of where should I put the peace of code you provide, or does my pattern relevant in PHP ?</p>