mysqli::close

mysqli_close

(PHP 5, PHP 7)

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

Description

Object oriented style

public mysqli::close(): bool

Procedural style

mysqli_close(mysqli $mysql): bool

Closes a previously opened database connection.

Open non-persistent MySQL connections and result sets are automatically closed when their objects are destroyed. Explicitly closing open connections and freeing result sets is optional. However, it's a good idea to close the connection as soon as the script finishes performing all of its database operations, if it still has a lot of processing to do after getting the results.

Parameters

mysql

Procedural style only: A mysqli object returned by mysqli_connect() or mysqli_init()

Return Values

Returns true on success or false on failure.

Examples

Example #1 mysqli::close() example

Object oriented style

<?php

mysqli_report
(MYSQLI_REPORT_ERROR MYSQLI_REPORT_STRICT);
$mysqli = new mysqli("localhost""my_user""my_password""world");

$result $mysqli->query("SELECT Name, CountryCode FROM City ORDER BY ID LIMIT 3");

/* Close the connection as soon as it's no longer needed */
$mysqli->close();

foreach (
$result as $row) {
    
/* Processing of the data retrieved from the database */
}

Procedural style

<?php

mysqli_report
(MYSQLI_REPORT_ERROR MYSQLI_REPORT_STRICT);
$mysqli mysqli_connect("localhost""my_user""my_password""world");

$result mysqli_query($mysqli"SELECT Name, CountryCode FROM City ORDER BY ID LIMIT 3");

/* Close the connection as soon as it's no longer needed */
mysqli_close($mysqli);

foreach (
$result as $row) {
    
/* Processing of the data retrieved from the database */
}

Notes

Note:

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

See Also

add a note add a note

User Contributed Notes 3 notes

up
1
PD
5 years 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
-18
Francois
5 years 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
-28
php at dafydd dot com
15 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