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.
*或者当请求结束时。我一时想不起来了。