4

Is there any method to close connection similar to mysql_close() for wpdb in WordPress? Is it not necessary to close connection for global wpdb?

0

1 Answer 1

8

WP 4.5 had introduced wpdb->close() method to close connection in current instance.


Old answer

There is no explicit method. It stores link identifier in $wpdb->dbh field, but it is protected, so not convenient to run close on.

7
  • means it is not needed to close global wpdb connection????
    – Brij
    Feb 1, 2011 at 9:38
  • @Brij unless I am missing something it should be closed automatically when page finishes loading and PHP ends execution.
    – Rarst
    Feb 1, 2011 at 9:53
  • "This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0."
    – jgraup
    Nov 2, 2016 at 2:30
  • 1
    @jgraup cheers, updated answer with info about new method which handles that.
    – Rarst
    Nov 2, 2016 at 9:32
  • 1
    @D.JCode if you are reusing global $wpdb you definitely shouldn't close it, it's just a single connection for everything and not a problem. You would only want to explicitly close if you are creating a lot of new wpdb instances (not something I ever seen done in WP).
    – Rarst
    Dec 23, 2020 at 18:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.