何时调用mysqli :: close

编程入门 行业动态 更新时间:2024-10-17 05:30:06
本文介绍了何时调用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

本文发布于:2023-10-09 05:47:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1474871.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:mysqli   close

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!