mysqli::close

mysqli_close

(PHP 5, PHP 7)

mysqli::close -- mysqli_closeCierra una conexión previamente abierta a una base de datos

Descripción

Estilo orientado a objetos

mysqli::close(): bool

Estilo por procedimientos

mysqli_close(mysqli $link): bool

Cierra una conexión abierta previamente a base de datos.

Las conexiones de MySQL no persistentes y los conjuntos de resultados son automáticamente destruidos cuando un script de PHP finaliza su ejecución. Por tanto, aunque el cierre explícito de conexiones abiertas y la liberación de conjuntos de resultados sean opcionales, se recomienda hacerlos. Así, se devolverán inmediatamente los recursos a PHP y a MySQL, lo que puede mejorar el rendimiento. Para información detalla, véase la liberación de recursos

Parámetros

link

Sólo estilo por procediminetos: Un identificador de enlace devuelto por mysqli_connect() o mysqli_init()

Valores devueltos

Devuelve true en caso de éxito o false en caso de error.

Ejemplos

Veasé mysqli_connect().

Notas

Nota:

mysqli_close() no cerrará las conexiones persistentes. Para más detalles, véase la página del manual sobre conexiones persistentes.

Ver también

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