热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

为什么我们关闭Mysqli-WhydowecloseresultinMysqli

Whyareweclosingthe$result为什么我们要关闭$result$mysqlinewmysqli(localhost,root,root

Why are we closing the $result

为什么我们要关闭$result

    $mysqli = new mysqli("localhost", "root", "root", "test");
    if ($mysqli->connect_errno) {
        echo "Failed to connect to MySQL: " . $mysqli->connect_error;
    }

    if ($result = $mysqli->query("Select * from user")) {
        while ($row = $result->fetch_object())
        {
            //var_dump($row);
        }
        $result->close();
    }   

1 个解决方案

#1


4  

Your check if the connection failed falls through to the code that uses the connection. Obviously that won't work because it's not a live connection. Make sure that if the connection fails, code that depends on it is not reached. (Note that I'm not necessary advocating use of exit or die. They can make for a rather bad user experience. They can be useful, but ideally you should output a better message and leave the ugly error stuff for logs [unless you're only testing or it's a command line script]).

如果连接失败,您将检查使用该连接的代码。很明显,这不会起作用,因为它不是一个实时连接。确保如果连接失败,则不会访问依赖于它的代码。请注意,我并不需要提倡使用退出或死亡。它们会造成相当糟糕的用户体验。它们可能是有用的,但理想情况下,您应该输出更好的消息,并为日志留下难看的错误信息(除非您只是在测试或它是一个命令行脚本)。

As for close() (which is actually an alias for free()):

至于close()(实际上是free()的别名):

free() basically deallocates all of the stuff attached to result. You need to understand that there are two (simplification, but go with it) ways of retrieving rows:

free()基本上处理与结果相关的所有内容。您需要理解有两种方式(简化,但随它去)检索行:

Buffered - Everything is snatched in one go. In other words, by the time you start looping through the records, they're all actually already in memory. They are buffered. They are not being pulled from the server every time you call fetch() but rather are being pulled from memory.

缓冲-所有东西都一次被抓取。换句话说,当你开始循环遍历记录时,它们实际上都已经在内存中了。他们是缓冲。它们不会在每次调用fetch()时从服务器中提取,而是从内存中提取。

Non buffered - there may be a small buffer, but essentially every time you call fetch(), PHP must pull a new row from the database. At the beginning of the loop, they are not all sitting in memory.

非缓冲—可能有一个小的缓冲区,但实际上每次调用fetch()时,PHP必须从数据库中提取一个新行。在循环的开始,它们并不都在内存中。

Interesting note: num_rows() can be called on a buffered query very efficiently since all of the rows are already pulled (though you should never pull all of the rows just to count them -- that's what COUNT is for). Non buffered queries cannot do num_rows() until they pull all of the rows. Meaning it will either be an error, or will essentially turn it into a buffered query.

有趣的注释:num_rows()可以非常有效地调用一个缓冲查询,因为所有的行都已经被拉了(尽管您不应该把所有的行都拉到一起来计算它们——这就是count的作用)。非缓冲查询不能执行num_rows(),直到它们拉出所有的行。这意味着它要么是一个错误,要么本质上将它转换为缓冲查询。

Anyway, back to your actual question:

不管怎样,回到你真正的问题:

free() frees anything associated with the result object. In the case of a buffered query, this is any rows sitting in memory. In the case of a non buffered query, free() will release whatever rows may be sitting in memory and then cancel the rest of the request. Basically its PHP telling MySQL, "Hey you know all those rows I request? Changed my mind. You can just drop that request."

free()释放与结果对象关联的任何内容。对于缓冲查询,这是内存中的任何行。对于非缓冲查询,free()将释放内存中可能存在的任何行,然后取消请求的其余部分。它的PHP告诉MySQL,嘿,你知道我请求的所有行吗?改变了我的想法。你可以放弃这个请求。

As for if you should free results... Well, whenever the variable goes out of scope*, it will happen anyway. There is, however, no harm in explicitly doing it, and in situations where a query may use a lot of memory and then another query after it may use a lot of memory, you may wish to free the sets just to keep memory usage low.

至于你是否应该得到免费的结果……当变量超出范围*时,它就会发生。然而,显式地这样做并没有什么害处,而且在查询可能使用大量内存,然后在查询可能使用大量内存之后再使用另一个查询的情况下,您可能希望释放这些集,以保持低内存使用率。

* Or maybe when the request ends. I don't remember off the top of my head.

*或者当请求结束时。我一时想不起来了。


推荐阅读
  • 本文详细介绍了 Dockerfile 的编写方法及其在网络配置中的应用,涵盖基础指令、镜像构建与发布流程,并深入探讨了 Docker 的默认网络、容器互联及自定义网络的实现。 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • DNN Community 和 Professional 版本的主要差异
    本文详细解析了 DotNetNuke (DNN) 的两种主要版本:Community 和 Professional。通过对比两者的功能和附加组件,帮助用户选择最适合其需求的版本。 ... [详细]
  • 本文详细探讨了JDBC(Java数据库连接)的内部机制,重点分析其作为服务提供者接口(SPI)框架的应用。通过类图和代码示例,展示了JDBC如何注册驱动程序、建立数据库连接以及执行SQL查询的过程。 ... [详细]
  • 优化局域网SSH连接延迟问题的解决方案
    本文介绍了解决局域网内SSH连接到服务器时出现长时间等待问题的方法。通过调整配置和优化网络设置,可以显著缩短SSH连接的时间。 ... [详细]
  • 通过Web界面管理Linux日志的解决方案
    本指南介绍了一种利用rsyslog、MariaDB和LogAnalyzer搭建集中式日志管理平台的方法,使用户可以通过Web界面查看和分析Linux系统的日志记录。此方案不仅适用于服务器环境,还提供了详细的步骤来确保系统的稳定性和安全性。 ... [详细]
  • 本文详细介绍了 org.apache.commons.io.IOCase 类中的 checkCompareTo() 方法,通过多个代码示例展示其在不同场景下的使用方法。 ... [详细]
  • 本文详细介绍了如何在 MySQL 中授予和撤销用户权限。包括创建用户、赋予不同级别的权限(如表级、数据库级、服务器级)、使权限生效、查看用户权限以及撤销权限的方法。此外,还提供了常见错误及其解决方法。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • 本文详细介绍了macOS系统的核心组件,包括如何管理其安全特性——系统完整性保护(SIP),并探讨了不同版本的更新亮点。对于使用macOS系统的用户来说,了解这些信息有助于更好地管理和优化系统性能。 ... [详细]
  • 在维护公司项目时,发现按下手机的某个物理按键后会激活相应的服务,并在屏幕上模拟点击特定坐标点。本文详细介绍了如何使用ADB Shell Input命令来模拟各种输入事件,包括滑动、按键和点击等。 ... [详细]
  • MySQL索引详解与优化
    本文深入探讨了MySQL中的索引机制,包括索引的基本概念、优势与劣势、分类及其实现原理,并详细介绍了索引的使用场景和优化技巧。通过具体示例,帮助读者更好地理解和应用索引以提升数据库性能。 ... [详细]
  • 本文将介绍网易NEC CSS框架的规范及其在实际项目中的应用。通过详细解析其分类和命名规则,探讨如何编写高效、可维护的CSS代码,并分享一些实用的学习心得。 ... [详细]
  • 本文详细介绍如何利用已搭建的LAMP(Linux、Apache、MySQL、PHP)环境,快速创建一个基于WordPress的内容管理系统(CMS)。WordPress是一款流行的开源博客平台,适用于个人或小型团队使用。 ... [详细]
  • This pull request introduces the ability to provide comprehensive paragraph configurations directly within the Create Note and Create Paragraph REST endpoints, reducing the need for additional configuration calls. ... [详细]
author-avatar
手机用户2602889563
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有