mysqli_store_result()与mysqli_use_result() [英] mysqli_store_result() vs. mysqli_use_result()

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

问题描述

mysqli::store_result() PHP.net上的文档似乎对两者之间的区别非常含糊. mysqli::use_result() 页不提供任何代码示例,并将您链接到<在href ="http://www.php.net/manual/en/mysqli.multi-query.php"> mysqli::multi_query() 页中查找它们.在该页面中,给出了以下代码示例(有关完整代码,请参见页面):

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\n", $row[0]);
    }
    $result->free();
}
/* print divider */
if ($mysqli->more_results()) {
    printf("-----------------\n");
}

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 — Transfers a result set from the last query
  • mysqli_use_result — Initiate a result set retrieval

它们看起来像是两种不同的事物,并且根本没有像别名那样被带来.仔细观察,我发现 mysqli::use_result() -页面:$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()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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