mysqli - Do I really need to do $result->close();

2020-02-07 18:36发布

Just started using mysqli. If I'm working with small data sets on small websites (traffic-wise), do I really need to use these all the time?

$result->close(); 
$mysqli->close();

Also, for someone doing custom PHP and MySQL work without a framework, is mysqli the general preferred way of interacting with MySQL?

标签: php mysql mysqli
4条回答
兄弟一词,经得起流年.
2楼-- · 2020-02-07 19:11

You should get in the habit of doing cleanup right (calling close as soon as you're done), or the resource leaks can gradually accumulate until they impact performance.

As far as what DB layer, learning PDO should be worthwhile because it is well-designed and compatible with all the major databases.

查看更多
戒情不戒烟
3楼-- · 2020-02-07 19:15

I felt an update to this thread was needed...

According to current documentation, you should always use $mysql->kill() in addition to $mysql->close().

$thread = $mysqli->thread_id;
$mysqli->kill($thread);
$mysqli->close();

(As a side note, I asked Oracle developers about using PDO with MySQL and they discouraged it. They use MySQLi exclusively. PDO hasn't been maintained and it doesn't support many of MySQL's current features.) Edit: obsolete comment.

Edit: switched the statement order, as suggested.

查看更多
戒情不戒烟
4楼-- · 2020-02-07 19:26

It is a good practice to release resource early when it is no more needed, this may avoid resource peek out when there are more number of concurrent user accessing the same page

查看更多
放我归山
5楼-- · 2020-02-07 19:29

PHP will close all open files and DB connections at the end of the script. It's good practice to do it manually when you are done with the connections, but it's no disaster if you don't. If you have a DB connection that will be used throughout the whole script you can as well leave it open.

+1 on PDO

查看更多
登录 后发表回答