mysqli_store_result() 与 mysqli_use_result()

时间:2023-03-06
本文介绍了mysqli_store_result() 与 mysqli_use_result()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

mysqli::store_result()mysqli::use_result()?

PHP.net 上的文档似乎对两者之间的区别非常模糊.mysqli::use_result()-page 不提供任何代码示例,并将您链接到 mysqli::multi_query()-page 来查找它们.在该页面中给出了以下代码示例(完整代码请参见页面):

The documentation on PHP.net seems very vague about the difference between the two. The mysqli::use_result()-page does not offer any code-samples, and links you to the mysqli::multi_query()-page to look for them. In that page the following code-sample is given (see the page for the full code):

/* store first result set */
if ($result = $mysqli->store_result()) {
    while ($row = $result->fetch_row()) {
        printf("%s
", $row[0]);
    }
    $result->free();
}
/* print divider */
if ($mysqli->more_results()) {
    printf("-----------------
");
}

mysqli::store_result()页面使用完全相同的代码示例,只有一个例外:

The mysqli::store_result()-page uses exactly the same code-sample, with one exception:

/* store first result set */
if ($result = $mysqli->use_result()) {

是的... store_result 变成了 use_result.请注意,即使上面的评论仍在说商店".

Yeah... store_result became use_result. Note that even the comment above is still saying "store".

看过代码示例后,我想;好吧,所以它是一个别名".可是等等!文档给出了以下描述:

Having seen the code samples, I thought; "all right, so it's an alias". But wait! The documentation gives the following descriptions:

  • mysqli_store_result — 传输上次查询的结果集
  • mysqli_use_result — 启动结果集检索
  • mysqli_store_result — Transfers a result set from the last query
  • mysqli_use_result — Initiate a result set retrieval

它们看起来像是两种不同的东西,根本不像别名.仔细观察,我发现 mysqli 的代码示例中还有另一个异常::use_result()-page: $result->free(); 变成了 $result->close();.然而,我查明真相的希望很快就破灭了,当我发现在第二个代码示例(程序等效)的同一页面上,使用了 mysqli_free_result($result); 而不是预期的 mysqli_close_result($result);.

They seem like two different things, and are not brought like aliases at all. Taking a closer look I found out that there was yet another exception in the code-sample of the mysqli::use_result()-page: $result->free(); became $result->close();. However my hopes for finding out the truth were soon after shattered, when I found that on that same page in the second code sample (the procedural equivalent), mysqli_free_result($result); was used, and not the expected mysqli_close_result($result);.

推荐答案

mysqli::store_result() 将从 MySQL 服务器获取整个结果集,而 mysqli::use_result() 将一一获取行.

mysqli::store_result() will fetch the whole resultset from the MySQL server while mysqli::use_result() will fetch the rows one by one.

您链接到的 mysqli::use_result 文档中也提到了这一点:

This is also mentioned in the mysqli::use_result docs you linked to:

mysqli_use_result() 函数不会从数据库传输整个结果集,因此不能使用诸如 mysqli_data_seek() 之类的函数移动到集中的特定行.要使用此功能,必须使用 mysqli_store_result() 存储结果集.如果在客户端执行大量处理,则不应使用 mysqli_use_result(),因为这会占用服务器并阻止其他线程更新任何从中获取数据的表.

The mysqli_use_result() function does not transfer the entire result set from the database and hence cannot be used functions such as mysqli_data_seek() to move to a particular row within the set. To use this functionality, the result set must be stored using mysqli_store_result(). One should not use mysqli_use_result() if a lot of processing on the client side is performed, since this will tie up the server and prevent other threads from updating any tables from which the data is being fetched.

您通常可以始终使用 mysqli::store_result() 除非您有充分的理由不一次从服务器读取所有行.

You can usually always use mysqli::store_result() unless you have a good reason for not reading all rows from the server at once.

这篇关于mysqli_store_result() 与 mysqli_use_result()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:PHP 在循环中准备语句和事务 下一篇:我应该什么时候关闭 PHP 中的数据库连接?

相关文章