When should I close a database connection in PHP?

后端 未结 2 407
自闭症患者
自闭症患者 2020-12-05 12:18

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 applica

相关标签:
2条回答
  • 2020-12-05 12:49

    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

    0 讨论(0)
  • 2020-12-05 12:55

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

    0 讨论(0)
提交回复
热议问题