Jo Smo
Jo Smo

Reputation: 7179

Do you need to call mysqli_stmt::close before you call mysqli::close?

I'm not exactly sure why mysqli_stmt::close is needed. Is it enough to just call mysqli::close?

Is it needed only when you want to execut multiple prepared statements? Or is it also needed if you execute just one?

Upvotes: 0

Views: 101

Answers (2)

Your Common Sense
Your Common Sense

Reputation: 158003

It all depends on the nature of your script.

If it's a regular web-page, that runs for a fraction of second, neither stmt::close nor mysqli::close is ever needed - PHP will close all the resources automatically.

While in a long-running PHP script it's good to close particular statements when you don't need them anymore, as every statement allocates some resources on the server that otherwise can be used by other connections.

Upvotes: 2

ChristianF
ChristianF

Reputation: 2061

Normally one doesn't need to close any database connections, or most other connections in PHP. As the garbage handler and the associated cleanup code does this automatically.

However, as noted in the comment by @Phil, sometimes you might want to close something earlier. Most common situations are either by not wanting the entire resultset, should a specific condition occur and you need to run another prepared statement; Or when you need to conserve resources.
Of these two the former is by far the most common, and even that isn't really common. So most of the time you do not need to worry about this.

Also, I recommend using the PDO library. It has a few more features than MySQLi, and a few other advantages over the older MySQLi-library.

Upvotes: 2

Related Questions