我应该什么时候关闭 PHP 中的数据库连接?

When should I close a database connection in PHP?(我应该什么时候关闭 PHP 中的数据库连接?)
本文介绍了我应该什么时候关闭 PHP 中的数据库连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我不像 php 开发人员,但我必须使用它,而且我并不真正了解 PHP 如何在会话期间处理内存分配.

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.

我正在开发一个要求 HTTP 身份验证的应用程序,一旦您登录,您就可以通过一个漂亮的界面操作数据.
有人在另一篇文章中告诉我,我不应该在每次执行后关闭 mysql 数据连接,但我不知道在使用这个应用程序期间这个连接是如何保持内存的,因为在服务器端我不知道 PHP 保留了什么记忆与否.

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:我正在使用 mysqli

PS: I'm using mysqli

编辑 1:
我的应用程序是用 MVC 模式设计的,意思是:

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

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

该模式允许视图仅通过 control.php 与数据交互,然后从 database.php 调用函数到 SELECTEDIT数据.根据你给我的解释,我应该说:

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);
}

database.php中,但是当需要选择或修改数据时加载该页面,所以它只执行很短的时间,这意味着它仍然会在结束时关闭连接请求.

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.

哪个给出了一个更精确的问题,即我应该将您提供的代码放在哪里,或者我的模式是否与 PHP 相关?

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

推荐答案

永远不要为每个查询都创建一个新的连接;您甚至不必手动关闭它.

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

看看:你如何在 php 中有效地连接到 mysql 而无需在每次查询时重新连接

你可以使用这个:

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

页面关闭时调用

这篇关于我应该什么时候关闭 PHP 中的数据库连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)