Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

When should i call mysqli::close? I never used to use if statements to check whether bind_param(), prep() and execute() were successful. Should I call $stmt->close() at the end of the method(below) . Or should I call it after every condition ensuring that I close the database connection even if the process fails at some stage e.g bind param.

public function function_name($id,$new_id ){
    $query = "UPDATE TABLE SET name = ? WHERE field = ? ";
    if($stmt=$this->prepare($query)){
        if($stmt->bind_param("is", $id, $new_id)){
            if($stmt->execute()){

            }else{//Could not execute the prepared statement
                $message = "Could not execute the prepared statement";
            }
        }else{//Could not bind the parameters
            $message = "Could not bind the parameters";
        }
    }else{
        $message = "Could not prepare the statement";
    }
    return $message
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
257 views
Welcome To Ask or Share your Answers For Others

1 Answer

When PHP exits it closes the database connections gracefully.

The only reason to use the close method is when you want to terminate a database connection that you′ll not use anymore, and you have lots of thing to do: Like processing and streaming the data, but if this is quick, you can forget about the close statement.

Putting it in the end of a script means redundancy, no performance or memory gain.

Whats is important: unset unused data, and if you will want to avoid memory leaks (which in my humble opnion are problem of PHP core in this case) use:

mysqli_kill();
mysqli_close(); 

This way the socket is killed too.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...