PHP - 关闭准备好的stmt

时间:2016-02-03 05:26:52

标签: php mysqli

我指的是this

我有类似

的东西
  function getData(){
     $query = "SELECT * FROM users WHERE emailID=?";
     $stmt = mysqli_prepare($query);
     if(false == $stmt){
         //handleerror Call
     }
     $bindParam = mysqli_stmt_bindParam($stmt,"s","a@a.com");
     if(false == $bindParam){
       **Do I need to close stmt here?**
       //handleError
     }
     $execute = mysqli_stmt_execute($stmt);
     if(false == $execute){
       **Do I need to close stmt here?**
       //handle error
     }
    **Or Stmt will automatically get closed at the end of a function?**
  }

有关如何关闭stmt的任何建议吗?

我怀疑我应该在哪里打电话?或者PHP会在函数结束时自动关闭每个语句吗?因为范围在那里结束。

我知道如何打电话。但我想知道我应该在这种情况下调用,还是在函数结束时自动关闭php。

2 个答案:

答案 0 :(得分:0)

BaseActivityClass

它将结束声明。

答案 1 :(得分:0)

如果您自己不关闭它,PHP将在脚本/函数结束时执行此操作。但你"应该"每当你不再需要它时关闭它。比方说,当发生错误时,您可能不会需要它。

function getData(){
 $query = "SELECT * FROM users WHERE emailID=?";
 $stmt = mysqli_prepare($query);
 if(false == $stmt){
     //handleerror Call
 }
 $bindParam = mysqli_stmt_bindParam($stmt,"s","a@a.com");
 if(false == $bindParam){
   //handleError
   mysqli_close_stmt($stmt)
   print "Binding failed";
 }
 $execute = mysqli_stmt_execute($stmt);
 if(false == $execute){
   mysqli_close_stmt($stmt)
   //handle error
 }
//if you haven't closed, PHP will close it now
}
相关问题