何时调用mysqli :: close [英] When to call mysqli::close

查看:50
本文介绍了何时调用mysqli :: close的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我什么时候应该调用mysqli :: close?我从未使用过if语句来检查bind_param(),prep()和execute()是否成功.我应该在method(下面)的末尾调用$ stmt-> 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
}

推荐答案

PHP退出时,它会正常关闭数据库连接.

When PHP exits it closes the database connections gracefully.

使用close方法的唯一原因是,当您要终止不再使用的数据库连接时,还有很多事情要做:处理和流传输数据,但是如果这样做很快,您可能会忘记结束声明.

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.

重要的是:重置未使用的数据,并且如果您想避免内存泄漏(在我的谦虚观点中,这是PHP核心的问题),请使用:

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.

这篇关于何时调用mysqli :: close的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆