MySQLi查询结果:最好的方法,您是否关闭,免费? [英] MySQLi query results: Best approach, do you close, free, both?

查看:45
本文介绍了MySQLi查询结果:最好的方法,您是否关闭,免费?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对使用 MySQLi ,查询以及相关的内存管理有一些疑问.这里的代码只是为了澄清我的问题,所以不要将其转储用于错误检查等.我知道这需要完成:)

I have some questions about using MySQLi, queries, and related memory management. The code here is just to clarify my questions, so don't dump on it for error checking, etc. I know that needs to be done :)

假设我有这样的东西:

@ $db = new mysqli($dbhost, $un, $ps, $dbname);
$query = "SELECT field1, field2 ".
         "FROM table1 ".
         "WHERE field1={$some_value}";
$results = $db->query($query);

while ($result = $results->fetch_object()) {
    // Do something with the results
}

$query = "SELECT field1, field2 ".
         "FROM table2 ".
         "WHERE field1={$some_value2}";
// question 1
$results = $db->query($query);

while ($result = $results->fetch_object()) {
    // Do something with the second set of results
}

// Tidy up, question 2
if ($results) {
    $results->free();
}
if ($db) {
    $db->close();
}

// Question 3, a general one

因此,根据上面代码中的注释,这是我的问题:

So, based on the comments in the code above, here are my questions:

  1. 当我将第二个查询的结果分配给$results时,与先前的结果相关联的内存发生了什么?在分配新结果之前,我应该释放该结果吗?

  1. When I assign the results of the second query to $results, what happens to the memory associated with the previous results? Should I be freeing that result before assigning the new one?

与1相关,当我最后进行清理时,仅清理最后一个结果就足够了吗?

Related to 1, when I do clean up at the end, is cleaning up just the last results enough?

当我尝试清理结果时,应该像上面那样释放它,还是应该关闭它,或者同时关闭两者?

When I do try to clean up a result, should I be freeing it as above, should I be closing it, or both?

我问问题3,因为mysqli::query PHP 文档中有一个使用close的示例,即使close不是mysqli_result的一部分(请参见 ).相比之下,我的普通PHP参考书使用的是free( PHP和MySQL Web开发,第四版,Welling和Thomson).

I ask question 3 because the PHP documentation for mysqli::query has an example that uses close, even though close is not part of mysqli_result (see example 1 in mysqli::query). And in contrast, my normal PHP reference text uses free (PHP and MySQL Web Development, Fourth Edition, Welling and Thomson).

推荐答案

当我分配结果时 第二个查询到$results,会发生什么 到与 以前的结果?

When I assign the results of the second query to $results, what happens to the memory associated with the previous results?

执行此操作时:

$results = $db->query($query);

如果$results中以前有内容,则无法再访问此旧内容,因为没有剩余的引用.

If there was something in $results before, this old content cannot be accessed anymore, as there is no reference left to it.

在这种情况下,PHP会将变量的旧内容标记为"不再需要",并且在PHP需要一些内存时将从内存中将其删除.

In such a case, PHP will mark the old content of the variable as "not needed anymore" -- and it will be removed from memory when PHP needs some memory.

至少对于一般的PHP变量而言,这是正确的;但是,对于SQL查询的结果,某些数据可能会保存在驱动程序级别的内存中,而PHP对此没有太多控制权.

This, at least, is true for general PHP variables; in the case of results from an SQL query, though, some data may be kept in memory on the driver-level -- over which PHP doesn't have much control.


我应该在释放结果之前 分配新的?

Should I be freeing that result before assigning the new one?

我从不这样做-但引用mysqli_result::free :

I never do that -- but, quoting the manual page of mysqli_result::free:

注意:您应该始终释放您的 mysqli_free_result()的结果,当 您不需要结果对象 不再

Note: You should always free your result with mysqli_free_result(), when your result object is not needed anymore

对于小的脚本来说可能并不重要...唯一可以确定的方法就是使用

It probably doesn't matter for a small script... And the only way to be sure would be to test, using memory_get_usage before and after calling that method, to see whether there is a difference or not.


与1有关,当我在 最后,只是清理最后一个 结果够吗?

Related to 1, when I do clean up at the end, is cleaning up just the last results enough?

脚本结束时:

  • 与数据库的连接将关闭-这意味着应释放驱动程序可能使用的所有内存
  • PHP脚本使用的所有变量将被销毁-这意味着应释放它们正在使用的内存.

因此,在脚本结尾处,可能实际上确实不需要释放结果集.

So, at the end of the script, there is probably really no need to free the resultset.


当我尝试清理结果时, 我应该像上面那样释放它吗? 我应该关闭它,还是两者都关闭?

When I do try to clean up a result, should I be freeing it as above, should I be closing it, or both?

如果关闭与数据库的连接(使用 mysqli::close (如您建议的那样),这将使您与数据库断开连接.

If you close the connection to the database (using mysqli::close like you proposed), this will disconnect you from the database.

这意味着如果要执行其他查询,则必须重新连接!这根本不好((需要一些时间,资源,...)

Which means you'll have to re-connect if you want to do another query! Which is not good at all (takes some time, resources, ... )

通常来说,在我确定不再需要数据库连接之前,我不会关闭它的连接-这意味着我不会在脚本结束之前断开连接.

Generally speaking, I would not close the connection to the database until I am really sure that I won't need it anymore -- which means I would not disconnect before the end of the script.

并且由于"脚本结尾"的意思是"连接将被关闭",即使您未指定该连接;我几乎从来没有自己关闭过连接.

And as "end of the script" means "the connection will be closed" even if you don't specify it; I almost never close the connection myself.

这篇关于MySQLi查询结果:最好的方法,您是否关闭,免费?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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