mysqli_store_result() 与 mysqli_use_result()

mysqli_store_result() vs. mysqli_use_result()(mysqli_store_result() 与 mysqli_use_result())
本文介绍了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()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)