为什么我们在Mysqli中关闭结果 [英] Why do we close result in Mysqli

查看:85
本文介绍了为什么我们在Mysqli中关闭结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们为什么要关闭$ result

Why are we closing the $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();
    }   

推荐答案

检查连接是否失败取决于使用该连接的代码.显然,这不起作用,因为它不是实时连接.确保如果连接失败,则无法访问依赖于该连接的代码. (请注意,我不必提倡使用exit或die.它们可以带来相当糟糕的用户体验.它们可能会很有用,但是理想情况下,您应该输出更好的消息,并为日志保留难看的错误内容[除非您只是测试,或者是命令行脚本]).

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

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

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

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:

已缓冲-一切都一口气被抢走了.换句话说,当您开始遍历记录时,它们实际上已经全部在内存中了.它们是缓冲的.并不是每次调用fetch()都会从服务器中将它们拉出,而是从内存中将它们拉出.

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()时,PHP必须从数据库中拉出新行.在循环开始时,它们并没有全部坐在内存中.

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.

有趣的是:num_rows()可以在缓冲查询中非常有效地调用,因为所有行都已经被拉出(尽管您永远不要拉所有的行只是为了对它们进行计数-这就是COUNT的目的).非缓冲查询在提取所有行之前不能执行num_rows().这意味着它要么是一个错误,要么本质上将其转换为缓冲的查询.

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.

无论如何,回到您的实际问题:

Anyway, back to your actual question:

free()释放与结果对象关联的所有内容.对于缓冲查询,这是内存中的所有行.对于非缓冲查询,free()将释放内存中可能存在的所有行,然后取消其余请求.基本上,它的PHP告诉MySQL,您知道我请求的所有行吗?改变了主意.您可以删除该请求."

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

至于您是否应该释放结果...那么,只要变量超出范围*,它将始终发生.但是,明确地执行此操作没有任何危害,并且在查询可能使用大量内存,然后又在查询之后使用大量内存的情况下,您可能希望释放这些集只是为了保持较低的内存使用率

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.

这篇关于为什么我们在Mysqli中关闭结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆