When to call mysqli::close

霸气de小男生 提交于 2019-11-29 14:46:18

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.

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 really are 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.

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

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!