Dutch PHP Conference 2025 - Call For Papers

mysql_close

(PHP 4, PHP 5)

mysql_closeClose MySQL connection

Увага

Це розширення застаріле, починаючи з PHP 5.5.0, та вилучене з PHP 7.0.0. Натомість використовуються розширення MySQLi або PDO_MySQL. Докладніше описано у керівництві MySQL: вибір API. Цю функцію можна замінити на:

Опис

mysql_close(resource $link_identifier = NULL): bool

mysql_close() closes the non-persistent connection to the MySQL server that's associated with the specified link identifier. If link_identifier isn't specified, the last opened link is used.

Відкриває непостійні з'єднання MySQL, а набори результатів автоматично знищує, коли PHP-скрипт завершується. Та незважаючи на те, що явне закриття з'єднань і очищення наборів результатів необов'язкове, робити це рекомендується. Це негайно вивільнить ресурси PHP та MySQL, а отже покращить продуктивність. Докладніше: Вивільнення ресурсів

Параметри

link_identifier

З'єднання MySQL. Якщо не задано, буде обрано останнє з'єднання, встановлене функцією mysql_connect(). Якщо з'єднання не вдалося знайти або встановити, буде виведено повідомлення рівня E_WARNING

Значення, що повертаються

Повертає true у разі успіху або false в разі помилки.

Приклади

Приклад #1 mysql_close() example

<?php
$link
= mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!
$link) {
die(
'Could not connect: ' . mysql_error());
}
echo
'Connected successfully';
mysql_close($link);
?>

Поданий вище приклад виведе:

Connected successfully

Примітки

Зауваження:

mysql_close() will not close persistent links created by mysql_pconnect(). For additional details, see the manual page on persistent connections.

Прогляньте також

add a note

User Contributed Notes 2 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
-5
mdes[SPAM]saintes at gmail dot com
14 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.
To Top