mysql_close

(PHP 3, PHP 4, PHP 5)

mysql_close -- 关闭 MySQL 连接

说明

bool mysql_close ( [resource link_identifier] )

如果成功则返回 TRUE,失败则返回 FALSE

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

通常不需要使用 mysql_close(),因为已打开的非持久连接会在脚本执行完毕后自动关闭。参见释放资源

注: mysql_close() 不会关闭由 mysql_pconnect() 建立的持久连接。

例子 1. MySQL 关闭例子

<?php
    $link
= mysql_connect("localhost", "mysql_user", "mysql_password")
        or die(
"Could not connect: " . mysql_error());
    print (
"Connected successfully");
    
mysql_close($link);
?>

参见 mysql_connect()mysql_pconnect()


add a note add a note User Contributed Notes
RonS
22-Aug-2006 07:38
I just got caught with the "connection to the same database" issue. I need to do a lookup to a master database for syncing issues, but there was a possibility a conenction to the master could be open elsewhere.  When I closed "my connection" it closed the "other connection" as well.

My goodness, this violates every principle in modern programming technique.  All functions can't possibly be smart enough to know if there is an existing connection to a database, and who the user is and all other parameters.

It is, however, good programming practice to close your database connections after you're done with them, a practice that I'll have to avoid allowing the program's termination to close the db since I can't tell if the return from mysql_connect() has given me my own resource or a pre-existing one.  Absolutely preposterous.

Since this is the lowest-level access to the database in the language, it should do as requested.
bbodelcampo at yahoo dot co dot uk
13-Dec-2005 09:20
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:

// 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....
4dacres at gmail dot com
13-Oct-2005 05:23
Read http://bugs.php.net/bug.php?id=30525, and wow... It might be 'just how it works', but this reads like a broken implementation. cameron2 has it right, with this implementation cleaning up connections with mysql_close() can never be done, esp. if the connection is passed to a routine that you have no visibilty into.  You just never know if setting the variable to null or calling mysql_close() is going to error the script.
levi at alliancesoftware dot com dot au
29-Apr-2005 03:03
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:
<?
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"
beer_nomaed _AT_ hotmail _DOT_ com
03-Dec-2004 05:26
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.