問題描述
在完成準備好的 mysqli 時,我試圖澄清 $stmt->close() 和 $stmt->free_result() 之間的區別強>聲明.
I am trying to clarify the difference between $stmt->close() and $stmt->free_result() when finalizing a prepared mysqli statement.
到目前為止我使用:
$mysqli = new mysqli(host,user,password,database);
$stmt = $mysqli->prepare(sql statement);
[...]
$stmt->free_result();
$mysqli->close();
一切似乎都正常.
但我見過很多程序員使用 $stmt->close 而不是 $stmt->free_result().一旦我看到了他們兩個:
But I've seen a lot of programmers use $stmt->close instead of $stmt->free_result(). And once I've seen both of them:
$stmt->free_result();
$stmt->close();
$mysqli->close();
那么我應該選擇什么,在什么情況下以及為什么?
So what should I choose, under which circumstances and why?
推薦答案
$stmt->free_result()
釋放與結果集相關的內存,而 $stmt->close()
釋放與準備好的語句相關的內存.隨后調用 $stmt->close()
將取消任何剩余的結果.
$stmt->free_result()
frees up memory related to a result set, whereas $stmt->close()
frees up memory related to a prepared statement. Subsequently calling $stmt->close()
will cancel any result still remaining.
本質上,調用 $stmt->close()
將提供與調用 $stmt->free_result()
相同的效果,因為它取消了結果集以及.但是調用 $stmt->free_result()
不會清除準備好的語句使用的內存,在這種情況下你必須使用 $stmt->close()
.
In essence, calling $stmt->close()
will provide the same effect as calling $stmt->free_result()
since it cancels the result set as well. But calling $stmt->free_result()
will not clear out the memory used by the prepared statement in which case you must use $stmt->close()
.
至于使用哪個 - 可能在某些情況下,您打算使用已初始化的準備好的語句,但不再需要您當前擁有的結果集.在這種情況下,您將等待調用 $stmt->close()
直到完成準備好的語句,然后調用 $stmt->free_result()
在執行另一個語句之前.
As far as which one to use goes - there may be situations where you intend on using the prepared statement you have initialized, but are no longer in need of the result set you currently have. In such a case you would wait on calling $stmt->close()
until you are done with the prepared statement and instead call $stmt->free_result()
before executing another statement.
這篇關于$stmt->close() vs $stmt->free_result()的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!