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
(PHP 5, PHP 7)
mysqli::close -- mysqli_close — Closes a previously opened database connection
Stil obiect-orientat
Stil procedural
Closes a previously opened database connection.
Conexiunile MySQL non-persistente deschise și seturile de rezultate sunt automat distruse atunci când un script PHP își încheie execuția. Deci închiderea în mod explicit a conexiunilor deschise și eliberarea seturilor de rezultate sunt opționale, însă sunt recomandata. Aceasta va întoarce imediat resursele către PHP și MySQL, ceea ce poate îmbunătăți performața. Pentru informații suplimentare accesați eliberarea resurselor
link
Numai stilul procedural: Un identificator al legăturii întors de mysqli_connect() sau mysqli_init()
Întoarce valoarea true
în cazul
succesului sau false
în cazul eșecului.
See mysqli_connect().
Notă:
mysqli_close() will not close persistent connections. For additional details, see the manual page on persistent connections.
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
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)
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.