note 52383 modified in function.mysql-close by danbrown

From: Date: Wed, 17 Jun 2009 13:55:51 +0000
Subject: note 52383 modified in function.mysql-close by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-156982@lists.php.net to get a copy of this message
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" --was-- 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" http://php.net/manual/en/function.mysql-close.php

« previous php.notes (#156982) next »