12

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.

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.
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.

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.

PS: I'm using mysqli

Edit 1:
My application is design with MVC pattern meaning :

|''''''''''|      |'''''''''''''|      |''''''''''''''|
| view.php |  ==> | control.php |  ==> | database.php |
|----------|      |_____________|      |______________|

That pattern allow the view to interact with data only through the control.php which then call a function from database.php to SELECT or EDIT data. with the explanation you give me, I should put:

public function __destruct(){
    mysql_close($this->connection);
}

inside database.php, 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.

Which gives a more precise question of where should I put the peace of code you provide, or does my pattern relevant in PHP ?

Kiwy
  • 340
  • 2
  • 10
  • 43
  • that's the destructor of your class performing the database managing, see: http://stackoverflow.com/questions/2129162/how-do-you-efficiently-connect-to-mysql-in-php-without-reconnecting-on-every-que – Gianluca Ghettini Dec 10 '13 at 11:13
  • that's because in a MVC pattern CONTROL controls VIEW and MODEL (database). In your scheme VIEW controls CONTROL and MODEL. If you code your MVC the right way the problem will solve automatically – Gianluca Ghettini Dec 10 '13 at 11:17
  • And here's the issue, I do not code with object because I don't really know how to do it and I don't have the time to convert the app neither the time to learn how it works. – Kiwy Dec 10 '13 at 11:17
  • ahah ok, nooo it's very simple to switch to OOP, many problems like this one will solve by itself with the right design :) – Gianluca Ghettini Dec 10 '13 at 11:19
  • In fact I used to be a java devolepper and the php is just making me crazy with the non typing variable and approximate things. So when I first tried to make some php Object, I started to get crazy. But thank for all the advise. my application will be for internal use only and without real issue about performance but I was curious about how it works. Now I know. – Kiwy Dec 10 '13 at 11:21
  • 1
    eheh I know, I think we all had that "bad feeling" when dealing with PHP the first time. What I most hate of PHP it's its loosely-type approach... sometimes it seems to program in BASIC :) – Gianluca Ghettini Dec 10 '13 at 11:24

2 Answers2

18

Never create a new connection for every query; You don't even have to close it manually.

Just create the connection at the beginning of you page

have a look at: How do you efficiently connect to mysql in php without reconnecting on every query

you can use this:

public function __destruct()
{
   mysql_close($this->connection);
}

it will get called when the page is closed

Community
  • 1
  • 1
Gianluca Ghettini
  • 11,129
  • 19
  • 93
  • 159
4

From PHP's Mysqli documentation:

a connection between a client process and a database can be reused by a client process, rather than being created and destroyed multiple times. This reduces the overhead...

EyalAr
  • 3,160
  • 1
  • 22
  • 30