2016-02-03 55 views
0

我指thisPHP - 关闭准备报告

我有类似

function getData(){ 
    $query = "SELECT * FROM users WHERE emailID=?"; 
    $stmt = mysqli_prepare($query); 
    if(false == $stmt){ 
     //handleerror Call 
    } 
    $bindParam = mysqli_stmt_bindParam($stmt,"s","[email protected]"); 
    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?** 
    } 

任何建议如何关闭语句?

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

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

+0

'mysqli_close_stmt($ stmt)'你在php手册页上没有理解什么? –

+0

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

+0

我不认为你需要手动关闭语句,只要你不需要获取大量数据并且需要内存。 –

回答

0
$stmt->close(); 

它将关闭该声明。

+0

他甚至不使用对象! –

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","[email protected]"); 
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 
}