問題描述
mysqli::store_result()
和 mysqli::use_result()
?
PHP.net 上的文檔似乎對兩者之間的區(qū)別非常模糊.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 — 傳輸上次查詢的結(jié)果集
- mysqli_use_result — 啟動結(jié)果集檢索
- mysqli_store_result — Transfers a result set from the last query
- mysqli_use_result — Initiate a result set retrieval
它們看起來像是兩種不同的東西,根本不像別名.仔細觀察,我發(fā)現(xiàn) mysqli 的代碼示例中還有另一個異常::use_result()
-page: $result->free();
變成了 $result->close();
.然而,我查明真相的希望很快就破滅了,當我發(fā)現(xiàn)在第二個代碼示例(程序等效)的同一頁面上,使用了 mysqli_free_result($result);
而不是預(yù)期的 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 服務(wù)器獲取整個結(jié)果集,而 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() 函數(shù)不會從數(shù)據(jù)庫傳輸整個結(jié)果集,因此不能使用諸如 mysqli_data_seek() 之類的函數(shù)移動到集中的特定行.要使用此功能,必須使用 mysqli_store_result() 存儲結(jié)果集.如果在客戶端執(zhí)行大量處理,則不應(yīng)使用 mysqli_use_result(),因為這會占用服務(wù)器并阻止其他線程更新任何從中獲取數(shù)據(jù)的表.
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()
除非您有充分的理由不一次從服務(wù)器讀取所有行.
You can usually always use mysqli::store_result()
unless you have a good reason for not reading all rows from the server at once.
這篇關(guān)于mysqli_store_result() 與 mysqli_use_result()的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!