search for in the  
<mysql_client_encodingmysql_connect>
Last updated: Thu, 19 May 2005

mysql_close

(PHP 3, PHP 4, PHP 5)

mysql_close -- Close MySQL connection

Description

bool mysql_close ( [resource link_identifier] )

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.

Using mysql_close() isn't usually necessary, as non-persistent open links are automatically closed at the end of the script's execution. See also freeing resources.

Parameters

link_identifier

The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments. If by chance no connection is found or established, an E_WARNING level warning is generated.

Return Values

Returns TRUE on success or FALSE on failure.

Examples

Example 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);
?>

The above example will output:

Connected successfully

Notes

Note: mysql_close() will not close persistent links created by mysql_pconnect().



User Contributed Notes
mysql_close
levi at alliancesoftware dot com dot au
29-Apr-2005 02: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"
red at redworld dot ru
31-Dec-2004 08:02
The mysql_close() call does seem to behave strangely when I make use of a nested call to a function that also connects to the database.

I am iterating through some results to a select, and with each row of the results I'm executing a separate function. This function also talks to the database and at the end of the parent iteration I get an error with the mysql_close() call. It seems to me that this is something in the region of issues with variable localisation.

It seems that MySQL doesn't mind if you don't close the link. This smells a little dangerous to me but when I removed the mysql_close() call at the end of the parent loop all worked.

Me thinks that things should not really be like this. I'm a child of perl ... there must be a way to open and close database connections without running into issues with multiple concurrent connections.
beer_nomaed _AT_ hotmail _DOT_ com
03-Dec-2004 03: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.

<mysql_client_encodingmysql_connect>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT