5
什麼是使用的重要性:PHP :::預處理語句::: freeresult()::: close()方法
$stmt->free_result();
$stmt->close();
使用準備好的statments這樣的數據庫調用後:
$mysqli=new mysqli("database", "db", "pass", "user");
$stmt = $mysqli->prepare("SELECT email FROM users WHERE id=? ");
$stmt->bind_param('i',$_SESSION['id']);
$stmt->execute();
$stmt->bind_result($email);
while($stmt->fetch()){
echo $email;
}
$stmt->free_result(); //why do i need this?
$stmt->close(); //why do i need this?
我問,因爲沒有他們,我沒有看到任何明顯的性能下降。難道這些命令通常僅用於當我保存使用結果:
$stmt->store_result();
像這樣:
$mysqli=new mysqli("database", "db", "pass", "user");
$stmt = $mysqli->prepare("SELECT email FROM users WHERE id=? ");
$stmt->bind_param('i',$_SESSION['id']);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($email);
while($stmt->fetch()){
echo $email;
}
$stmt->free_result(); //why do i need this?
$stmt->close(); //why do i need this?
最終問題歸結到何時是適當的時候使用freeresult()和close( )?
不,它並沒有告訴任何東西到數據庫 – zerkms
所以我可以用任何語句的結束freeresult(),我不需要storeresult()呢? –
@丹Kanze:只要你沒有遇到任何問題 - 不要這樣做,不要浪費CPU週期爲Nothing – zerkms