Cuando llamar a mysqli :: close

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
}

preguntado el 31 de enero de 12 a las 16:01

Unless you're in a long-running-script situation, don't bother doing a close operation, as PHP will clean up for you when the script exits. The connection shouldn't be closed until you're absolutely sure you're done with ALL database operations. -

@MarcB - I'm not sure I agree that it's good practice to do that. Whilst PHP will tear everything down automatically, getting into the habit of looking after your resources and disposing of them when you're done is a very important habit to maintain if you ever plan to move to the world of native programming. -

Incidentally, you should look into PHP exception handling rather than using a giant nest of if declaraciones: php.net/manual/en/class.exception.php -

4 Respuestas

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.

Respondido el 31 de enero de 12 a las 21:01

You should always gracefully close the connection when you are done with it, to avoid performance and memory/handle leak problems. On the other hand, you should also make sure that you realmente son done with it - your script will crash if you try to use a closed connection.

The same goes for statements. If a statement is no longer going to be used, dispose of it appropriately.

Respondido el 31 de enero de 12 a las 20:01

Just a little thought you could create another method called function_close and call it after you have called the function_name método.

This way if you change to pdo or mongo you will just have to refactor the methords rather than every instance of close.

Respondido el 31 de enero de 12 a las 20:01

I don't think he was asking about that. - Polinomio

Try just thought I would give him something to think about. - Oliver Bayes-Shelton

you should close it especially after a multi query (http://php.net/manual/en/mysqli.multi-query.php) because you could get memory problems. Otherwise closing is not needed.

Respondido el 31 de enero de 12 a las 21:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.