parent root
PHP: mysqli::close - Manual

mysqli::close

mysqli_close

(PHP 5, PHP 7)

mysqli::close -- mysqli_closeCloses a previously opened database connection

Description

Object oriented style

mysqli::close ( void ) : bool

Procedural style

mysqli_close ( mysqli $link ) : bool

Closes a previously opened database connection.

Open non-persistent MySQL connections and result sets are automatically destroyed when a PHP script finishes its execution. So, while explicitly closing open connections and freeing result sets is optional, doing so is recommended. This will immediately return resources to PHP and MySQL, which can improve performance. For related information, see freeing resources

Parameters

link

Procedural style only: A link identifier returned by mysqli_connect() or mysqli_init()

Return Values

Returns TRUE on success or FALSE on failure.

Examples

See mysqli_connect().

Notes

Note:

mysqli_close() will not close persistent connections. For additional details, see the manual page on persistent connections.

See Also

add a noteadd a note

User Contributed Notes 3 notes

up
0
PD
8 months ago
Note with Francis' example, using the function name link() will throw an error at runtime as it is already a function within the language. see: http://php.net/manual/en/function.link.php
up
-8
Francois
1 year ago
Since a lot of manual examples recommend to use a variable to initiate your connection, it is interesting to know that mysqli_close() will unset that variable, causing further connection attempts to fail.
ex:

$link = mysqli_connect($host, $user, $pw);

if ($link) {
    // Database is reachable
    mysqli_close($link);
}

if ($link) {
    // Database unreachable because $link = NULL
}

Easiest solution for me is to initiate connection through a function.
ex:

function link() {
    global $host;
    global $user;
    global $pw;
    global $link;
    $link = mysqli_connect($host, $user, $pw);
}

link();
// Database is reachable
mysqli_close($link)
link();
// Database is reachable
mysqli_close($link)
up
-25
php at dafydd dot com
10 years ago
I've had situations where database connections appeared to persist following php execution. So, now, my __destructor function explicitly contains a $cxn->close(). It hurts nothing, and helps avoid memory leaks.
To Top
parent root