SunshinePHP 2019

mysqli::close

mysqli_close

(PHP 5, PHP 7)

mysqli::close -- mysqli_close关闭先前打开的数据库连接

说明

面向对象风格

bool mysqli::close ( void )

过程化风格

bool mysqli_close ( mysqli $link )

关闭先前打开的数据库连接

参数

link

仅以过程化样式:由mysqli_connect()mysqli_init() 返回的链接标识。

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

See mysqli_connect().

参见

add a note add a note

User Contributed Notes 2 notes

up
0
Francois
6 months 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
-19
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