php mysqli_store_result() 与 mysqli_use_result()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9876730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mysqli_store_result() vs. mysqli_use_result()
提问by Avaq
The question
问题
What is the difference between mysqli::store_result()
and mysqli::use_result()
?
mysqli::store_result()
和 和有mysqli::use_result()
什么区别?
The story
故事
Vague documentation
含糊不清的文档
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):
PHP.net 上的文档似乎对两者之间的区别非常模糊。该mysqli::use_result()
-page不提供任何代码样本,你链接到mysqli::multi_query()
页随机寻找他们。在该页面中给出了以下代码示例(完整代码请参见页面):
/* 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");
}
The mysqli::store_result()
-page uses exactlythe same code-sample, with one exception:
该mysqli::store_result()
-page用途正是相同的代码样本,但有一个例外:
/* store first result set */
if ($result = $mysqli->use_result()) {
Yeah... store_result
became use_result
. Note that even the comment above is still saying "store".
是的……store_result
变成了use_result
。请注意,即使上面的评论仍在说“商店”。
Even more confusing
更让人迷惑
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_store_result— 传输上次查询的结果集
- mysqli_use_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::use_result()
-page:$result->free();
成了$result->close();
。然而,当我发现在第二个代码示例(程序等效)的同一页面上mysqli_free_result($result);
使用了mysqli_close_result($result);
.
回答by ThiefMaster
mysqli::store_result()
will fetch the whole resultset from the MySQL server while mysqli::use_result()
will fetch the rows one by one.
mysqli::store_result()
将从 MySQL 服务器获取整个结果集,同时mysqli::use_result()
将逐行获取行。
This is also mentioned in the mysqli::use_result
docs you linked to:
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_use_result() 函数不会从数据库传输整个结果集,因此不能使用诸如 mysqli_data_seek() 之类的函数移动到集中的特定行。要使用此功能,必须使用 mysqli_store_result() 存储结果集。如果在客户端执行大量处理,则不应使用 mysqli_use_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()
除非您有充分的理由不立即从服务器读取所有行。
回答by Naftali aka Neal
use_result
returns an unbufferedresult.
use_result
返回未缓冲的结果。
store_result
returns a bufferedresult.
store_result
返回缓冲结果。
回答by Christopher
I think this elaboration might help people getting here from languages that support retrieving either via arrays or enumerators. Because it is just that difference.
我认为这种阐述可能会帮助人们从支持通过数组或枚举器检索的语言到达这里。因为这就是差异。
For .NET, it would be the same difference as GetDirectories(store_results) vs EnumerateDirectories(use_results) (and the File/DbResult/CSV parsing/XML Parsing equivalents).
对于 .NET,它与GetDirectories(store_results) 与EnumerateDirectories(use_results)(以及 File/DbResult/CSV parsing/XML Parsing 等效项)具有相同的区别。
The enumerator variant uses less (PHP) client memory and might fetch the next row asynchronously in the background while the current row is processing, making it both more memory and processing time effective on the client side. At the cost of taking up (SQL) Server resources for longer (as the result set has not been fully transmitted).
枚举器变体使用较少的 (PHP) 客户端内存,并且可能会在处理当前行时在后台异步获取下一行,从而使其在客户端获得更多内存和处理时间。以占用 (SQL) 服务器资源更长的时间为代价(因为结果集尚未完全传输)。
In turn the get/array way will retrieve the data in one blocking operation, taking up more client side memory for freeing DB memory earlier.
反过来,get/array 方式将在一个阻塞操作中检索数据,占用更多的客户端内存,以便更早地释放 DB 内存。
Personally I would default for the non-Enumerator Way, unless you actually got (client) memory issues.
就个人而言,我会默认使用非枚举器方式,除非您确实遇到了(客户端)内存问题。
In .NET you have to use the Enumerator way, if you run into the 2/3 GiB limit for x32 processes. Particular EnumerateLines for files is important here. With DB results it should not happen to begin with. A 2 GiB DB result is either wayunder-filtered (do more filtering in Query) or contains a lot of Blobs (which you should load piecemeal or even let the browser retrieve via a http handler if possible).
在 .NET 中,如果遇到 x32 进程的 2/3 GiB 限制,则必须使用 Enumerator 方式。文件的特定 EnumerateLines 在这里很重要。对于 DB 结果,它不应该一开始就发生。A 2吉布DB结果要么方式下过滤(做多查询过滤)或含有大量的斑点(你应该加载零碎,甚至让如果可能的话,浏览器通过HTTP处理程序检索)。