php mysqli - 我真的需要做 $result->close(); & $mysqli->close();?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2879500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:00:42  来源:igfitidea点击:

mysqli - Do I really need to do $result->close(); & $mysqli->close();?

phpmysqlmysqli

提问by James

Just started using mysqli. If I'm working with small data sets on small websites (traffic-wise), do I really need to use these all the time?

刚开始使用mysqli。如果我在小型网站上使用小型数据集(流量方面),我真的需要一直使用这些吗?

$result->close(); 
$mysqli->close();

Also, for someone doing custom PHP and MySQL work without a framework, is mysqli the general preferred way of interacting with MySQL?

另外,对于在没有框架的情况下进行自定义 PHP 和 MySQL 工作的人来说,mysqli 是与 MySQL 交互的一般首选方式吗?

回答by Emil Vikstr?m

PHP will close all open files and DB connections at the end of the script. It's good practice to do it manually when you are done with the connections, but it's no disaster if you don't. If you have a DB connection that will be used throughout the whole script you can as well leave it open.

PHP 将在脚本结束时关闭所有打开的文件和数据库连接。完成连接后手动执行此操作是一种很好的做法,但如果不这样做也不是灾难。如果您有一个将在整个脚本中使用的数据库连接,您也可以将其保持打开状态。

+1 on PDO

在 PDO 上 +1

回答by Ixalmida

I felt an update to this thread was needed...

我觉得需要更新这个线程......

According to current documentation, you should always use $mysql->kill() in addition to $mysql->close().

根据当前的文档,除了 $mysql->close() 之外,您还应该始终使用 $mysql->kill()。

$thread = $mysqli->thread_id;
$mysqli->kill($thread);
$mysqli->close();

(As a side note, I asked Oracle developers about using PDO with MySQL and they discouraged it. They use MySQLi exclusively. PDO hasn't been maintained and it doesn't support many of MySQL's current features.) Edit: obsolete comment.

(作为旁注,我询问 Oracle 开发人员关于将 PDO 与 MySQL 一起使用的问题,他们不鼓励这样做。他们只使用 MySQLi。PDO 尚未得到维护,它不支持 MySQL 的许多当前功能。) 编辑:过时的评论。

Edit: switched the statement order, as suggested.

编辑:按照建议切换语句顺序。

回答by Matthew Flaschen

You should get in the habit of doing cleanup right (calling close as soon as you're done), or the resource leaks can gradually accumulate until they impact performance.

您应该养成正确清理的习惯(完成后立即调用 close),否则资源泄漏会逐渐累积,直到影响性能。

As far as what DB layer, learning PDOshould be worthwhile because it is well-designed and compatible with all the major databases.

至于什么DB层,学习PDO应该是值得的,因为它设计精良,兼容所有主流数据库。

回答by Raj

It is a good practice to release resource early when it is no more needed, this may avoid resource peek out when there are more number of concurrent user accessing the same page

在不再需要资源时尽早释放资源是一个很好的做法,这可以避免在有更多并发用户访问同一页面时资源偷看