Statement on glibc/iconv Vulnerability

mysql_close

(PHP 4, PHP 5)

mysql_closeCerrar una conexión de MySQL

Advertencia

Esta extensión fue declarada obsoleta en PHP 5.5.0 y eliminada en PHP 7.0.0. En su lugar debería utilzarse las extensiones MySQLi o PDO_MySQL. Véase también la guía MySQL: elegir una API. Las alternativas a esta función son:

Descripción

mysql_close(resource $link_identifier = NULL): bool

mysql_close() cierra la conexión no persistente al servidor de MySQL que está asociada con el identificador de enlace especificado. Si link_identifier no se especifica, se usará el último enlace abierto.

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_identifier

La conexión MySQL. Si no se especifica el enlace, se asumirá el último enlace abierto por mysql_connect(). Si no se encuentra o no se puede establecer la conexión se producirá un error del nivel E_WARNING.

Valores devueltos

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

Ejemplos

Ejemplo #1 Ejemplo de mysql_close()

<?php
$enlace
= mysql_connect('localhost', 'usuario_mysql', 'contraseña_mysql');
if (!
$enlace) {
die(
'No se pudo conectar: ' . mysql_error());
}
echo
'Conectado con éxito';
mysql_close($enlace);
?>

El resultado del ejemplo sería:

Conectado con éxito

Notas

Nota:

mysql_close() no cerrará los enlaces persistentes creados por mysql_pconnect(). Para más detalles, véase la página del manual sobre conexiones persistentes.

Ver también

add a note

User Contributed Notes 4 notes

up
5
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
-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
-9
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