mysql_close

(PHP 4, PHP 5)

mysql_close关闭 MySQL 连接

警告

本扩展自 PHP 5.5.0 起已废弃,并在自 PHP 7.0.0 开始被移除。应使用 MySQLiPDO_MySQL 扩展来替换之。参见 MySQL:选择 API 指南来获取更多信息。用以替代本函数的有:

说明

mysql_close(resource $link_identifier = NULL): bool

mysql_close() 关闭指定的连接标识所关联的到 MySQL 服务器的非持久连接。如果没有指定 link_identifier,则关闭上一个打开的连接。

建立非持久性的 MySQL 连接,会在 PHP 执行完毕后自动销毁,所以手动关闭连接,并释放资源是可选的。但是还是推荐你在代码中显性的执行该操作,这对提高代码性能有帮助。参考文档 资源释放

参数

link_identifier

MySQL 连接. 如果该连接标识符未给出, 将使用最近一次mysql_connect()建立的连接. 如果没有找到可使用的连接, 将产生一个 E_WARNING 错误.

返回值

成功时返回 true, 或者在失败时返回 false

示例

示例 #1 mysql_close() 示例

<?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() 不会关闭由 mysql_pconnect() 建立的持久连接。 有关其他详细信息,请参阅有关持久连接的手册页。

参见

添加备注

用户贡献的备注 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