mysql_close

(PHP 4, PHP 5)

mysql_closeZamyka połączenie MySQL

Ostrzeżenie

To rozszerzenie zostało uznane za przestarzałe w PHP 5.5.0 i usunięte w PHP 7.0.0. Zamiast niego należy stosować rozszerzenia MySQLi lub PDO_MySQL. Zobacz też przewodnik MySQL: wybieranie API i stosowne FAQ aby uzyskać więcej informacji. Alternatywy dla tej funkcji to między innymi:

Opis

mysql_close ([ resource $identyfikator_połączenia = NULL ] ) : bool

mysql_close() zamyka niestałe połączenie do serwera MySQL, które zostało skojarzone z podanym identyfikatorem połączenia. Jeśli identyfikator_połączenia nie został określony, zostanie użyte ostatnio używane połączenie.

Otwarte nietrwałe połącznie z MySQL i zbiory wyników są automatycznie niszczone, gdy skrypt PHP kończy swoje wykonywanie. Tak więc, mimo iż samodzielne zamykanie połączeń i zwalnianie zasobów wyników nie jest obowiązkowe, jest to zalecane. Pozwala natychmiastowo zwrócić zasoby dla PHP i MySQL, co może poprawić wydajność. Aby uzyskać więcej informacji, przeczytaj o zwalnianiu zasobów

Parametry

link_identifier

Połączenie MySQL. Jeśli identyfikator połączenia nie zostanie podany, użyte zostanie ostatnie połączenie otwarte za pomocą funkcji mysql_connect(). Jeśli nie znalezione zostanie żadne połączenie, wygenerowany zostanie błąd poziomu E_WARNING.

Zwracane wartości

Zwraca TRUE w przypadku powodzenia, FALSE w przypadku błędu.

Przykłady

Przykład #1 Przykład mysql_close()

<?php
$link 
mysql_connect('localhost''użytkownik_mysql''hasło_mysql');
if (!
$link) {
    die(
'Nie można się połączyć: ' mysql_error());
}
echo 
'Połączono poprawnie';
mysql_close($link);
?>

Powyższy przykład wyświetli:

Połączono poprawnie

Notatki

Informacja:

mysql_close() nie zamknie stałych połączeń stworzonych przez mysql_pconnect(). Aby uzyskać więcej informacji, zobacz stronę podręcznika poświęconą połączeniom stałym.

Zobacz też:

add a note add a note

User Contributed Notes 6 notes

up
8
bbodelcampo at yahoo dot co dot uk
18 years ago
A little note about multiple simultaneous connections to different hosts...

I work on a site that pulls content primarily from one db but uses a db on a foreign server to verify licensing.  One might expect the following to work:

<?php
// Open the connection to the primary db
$res1 = mysql_connect($host1, $user1, $pass1);
mysql_select_db($db1);

// Open connection to the license server
$res2 = mysql_connect($host2, $user2, $pass2);
mysql_select_db($db2, $res2);

// Pull license data and close when done
mysql_query($check_sql, $res2);
// ...
mysql_close($res2);

// Now pull content from the primary db
// Not specifying the resource should default to the last open db
mysql_query($query);
// ...
?>

Turns out this last query, since it cant find an active connection, will try to connect with mysql_connect() with no paramaters.  But if instead you do it as mysql_query($query, $res1), or alternatively, run the mysql_connect for this host again then it works fine.  Thus, it doesnt seem to be possible to have code with an overarching "global" db connection interspersed with temporary connections to another host/db....
up
-5
beer_nomaed _AT_ hotmail _DOT_ com
19 years ago
Be careful when using multiple links to connect to same database (with same username). Unless you specify explicitly in mysql_connect() to create a new link, it will return an already open link. If that would be closed by mysql_close(), it will also (obviously) close the other connection, since the link is the same.
Had lot of trouble figuring it out, since in <=4.3.6 there was a bug which didn't close the connection, but after the patch to >=4.3.7, all my application broke down because of a single script that did this.
up
-6
mdes[SPAM]saintes at gmail dot com
13 years ago
i just came over a problem that i had with apache.

It crashs and said :

"Parent: child process exited with status 3221225477 -- Restarting."

the error came from the extesion php_mysql.dll

i didn't understand what was the reason of that crash..

Then, i debug the script that i had downloaded and i noticed that that was the function mysql_close() which caused the problem.

The solution is, to send to it the link identifier which is optionnal in the description but cause a crash with no commentary.

Thanks to agneady.
up
-7
Anonymous
14 years ago
At least with PHP5.3.2 and Windows connecting by tcp, you should always use this mysql_close() function to close and free up the tcp socket being used by PHP.  Garbage collection after script execution does not close the tcp socket on its own.  The socket would otherwise remain in 'wait' state for approximately 30 seconds, and any additional page loads/connection attempts would only add to the total number of open tcp connections.  This wait time does not appear to be configurable via PHP settings.
up
-8
levi at alliancesoftware dot com dot au
18 years ago
As at 5.0.x and 4.3.x: This function should never be used with shared links; instead you should set your link variables to null.
(This explains red's and beer's () problems in previous comments)

  Here is how shared links work:
  - Each link is a resource. mysql_connect() by default looks for a resource with the same paramaters. If one exists, it will return the existing resource.
  - Every assignment of that resource to a variable increases the resource's reference count.
  - When the reference is decremented to zero, the underlying TCP/socket connection is closed.
    - Every assignment of a variable away from that resource decrements the reference count. (This includes a function level variable going out of scope)
    - mysql_close() also decrements the reference count.

Note the last two points: mysql_close() _and_ reassignment of a variable decrement the link's reference count.

A common mistake is a function like:

<?php
function dothings() {
 
$link = mysql_open(...);
 
// .. do some queries ..
 
mysql_close($link)
 
$link = null;
}
?>

this will decrement the counter twice, possibly closing the underlying connection and causing errors in other parts of the program.

http://bugs.php.net/bug.php?id=30525 "this is not a bug but just how it works"
up
-10
omega99999 at gmail dot com
13 years ago
The variable is definitely not optional in 5.3... Caused me a bit of a headache when I was debugging until I realized it was the close function that was causing a hang. So if using just:

<?php
mysql_connect
(<...>);

mysql_close();
?>

Use:

<?php
$connection
= mysql_connect(<...>);

mysql_close($connection);
?>

(where $connection is any variable of your choice)
To Top