downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

mysql_connect> <mysql_client_encoding
Last updated: Fri, 24 Jul 2009

view this page in

mysql_close

(PHP 4, PHP 5)

mysql_closeMySQL 접속을 닫음

설명

bool mysql_close ([ resource $link_identifier ] )

mysql_close()link_identifier 와 연관된 MySQL서버의 비영속적 접속을 종료한다. link_identifier 가 기술되지 않으면 최근 접속된 연결을 사용한다.

mysql_close()는 비영구적으로 열려진 링크에서는 일반적으로 스크립트 실행 마지막 부분에서 자동으로 종료되므로 사용할 필요가 없다. resource 해제하기.

인수

link_identifier

MySQL 연결. 지정하지 않으면 mysql_connect()로 연 마지막 연결을 사용합니다. 연결이 없으면, 인수 없이 mysql_connect()를 호출하여 연결을 만듭니다. 연결이 성립되지 않으면 E_WARNING 등급의 오류를 생성합니다.

반환값

성공할 경우 TRUE를, 실패할 경우 FALSE를 반환합니다.

예제

Example #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

주의

Note: mysql_close()mysql_pconnect()에 의해 영속적으로 연결된 접속은 종료하지 않는다.

참고



mysql_connect> <mysql_client_encoding
Last updated: Fri, 24 Jul 2009
 
add a note add a note User Contributed Notes
mysql_close
agneady (nospam please) at gmail dot com
11-Oct-2009 09:09
It seems that as of PHP 5.3, the link_identifier parameter is no longer optional. If not included, it would cause the script to crash with no errors in the output.
bbodelcampo at yahoo dot co dot uk
13-Dec-2005 01: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:

<?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....
levi at alliancesoftware dot com dot au
29-Apr-2005 07: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:

<?php
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 09: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_connect> <mysql_client_encoding
Last updated: Fri, 24 Jul 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites