Bug #15261 cannot close connection to database with function mysql_close;
Submitted: 26 Nov 2005 14:03 Modified: 27 Nov 2005 15:01
Reporter: Sebastian Miotk Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server Severity:S2 (Serious)
Version:4.0.25 OS:
Assigned to: CPU Architecture:Any

[26 Nov 2005 14:03] Sebastian Miotk
Description:
I have problem with mysql_close
this function must be near mysql_connect(). 
4 example: 

function connect() {
$dbh=mysql_connect ("localhost", "user", "passwd") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("database_name", $dbh); 
}
-----------------------------------
in program connect();
-----------------------------------
and at th end of work with database:
mysql_close($dbh); report bug that $dbh is not valid link to connection of mysql

but if i give only mysql_close(); than is everything ok.
but i didnt watch that the function close the connection automaticaly. Mayby someone else can give me advice?

How to repeat:
use the mysql_close($name_of__connection_link_from_y_function);
at the end of PHP5 program 4 example mysql_connect(); at the begin of program and mysql_close(); at the end of program
[26 Nov 2005 14:14] Valeriy Kravchuk
Thank you for a problem report. Please, specify the version of MySQL server your are working with. Try to create and send the simplest but complete PHP script that demostrates the problem you described.
[26 Nov 2005 16:43] Sebastian Miotk
here modul: polacz_baze.inc
<?
function connect() {
$dbh=mysql_connect ("localhost", "user", "passwd") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("name_db", $dbh); 
}
?>
------------------------------------------
here program:

<?
include(polacz_baze.inc);
connect();
?>
---

here some procedures and function

4 example:
					<?
							if(!$miasto):
					?>
<table width="520" cellpadding="0" cellspacing="0">
<tr>
<td width="520" class="naglowek" align="center">
<strong>Szukaj hotelu za pomoc± Mapy Polski:</strong>
</td>
</tr>
					<?
							/* w razie jakby mi sie zachcialo cos dodac*/
						
						printf("&nbsp;");

							else:

						$sql="SELECT woj.nazwa FROM woj LEFT JOIN miasto ON woj.id_woj=miasto.id_woj WHERE miasto LIKE '$miasto'";
								$wynik1 = mysql_query($sql);
								$row = mysql_fetch_array($wynik1);	
						$wojewodztwo=$row["nazwa"];

						$sql="SELECT reg.nazwa FROM woj LEFT JOIN reg ON woj.id_reg=reg.id_reg WHERE woj.nazwa LIKE '$wojewodztwo'";
								$wynik = mysql_query($sql);
								$row = mysql_fetch_array($wynik);	
						$region=$row["nazwa"];
						printf("<p class=\"opis\">Szukasz noclegów: w miejscowosci: <bold><strong><font color=\"red\"> %s</font></strong> </bold> <br> województwo - <bold><font color=\"navy\"> %s </font></bold> - region - <bold><font color=\"navy\"> %s </font></bold> </p> \n", $miasto, $wojewodztwo, $region);
						$sql="SELECT Count(*) FROM obiekt LEFT JOIN miasto ON obiekt.id_miasto=miasto.id_miasto WHERE miasto.miasto LIKE '$miasto'";
								$wynik = mysql_query($sql);
								$row = mysql_fetch_array($wynik);

							$licznik= $row["Count(*)"];
							if($licznik<'1'):
							printf("Niestety <font color=\"red\">nie znaleziono żadnych </font> obiektów spełniaj±cych powyzsze kryteria. Spróbuj w innej miejscowo¶ci.");
							endif;
							if($licznik=='1'):
							printf("Znaleziono <font color=\"red\">jeden </font> obiekt spełniaj±cy powyższe kryteria.");
							endif;

							if(($licznik >'1')&&($licznik<'4')):
							printf("Znaleziono <font color=\"red\">%s </font> obiekty spełniaj±ce powyższe kryteria.", $licznik);
							endif;
							if($licznik>'4'):
							printf("Znaleziono <font color=\"red\">%s </font> obiektów spełniaj±cych powyższe kryteria.", $licznik);
							endif;
						
						?>

------------------------------------------------------
bla bla bla............... in html and others function in php

------------------------------------------------------

and  at the and of html 
<?
mysql_close($dbh);
/*HERE I HAVE ERROR  but if I give mysql_close(); Everything is ok but I dont know that the connection is close automaticly like when open function mysql_connect();*/
?>

before 
</table></body></html>
[27 Nov 2005 9:57] Valeriy Kravchuk
I am not good at all in PHP, but for me it looks like your $dbh is a _local_ variable of your connect function. So, you surely got a problem when you try to use it later in mysql_close. 

Please, check with a very simple code, in one file, like presented at http://php.net/manual/en/function.mysql-connect.php. Just mysql_connect, simple mysql_query and mysql_close. Inform about the results. 

And, please, rememeber, that you have to use mysqli_* functions in PHP to be able to work with stored procedures and other new features of MySQL 5.
[27 Nov 2005 15:01] Sebastian Miotk
ha ha ha 
yea Y have right!
that $dbh was in function and was local var
anyway mysql_close(); was finish last connect and last connect was $dbh
I was blind maybe too much beer when I was programing ;)
Y are best 

sory 4 your time
Thanks
Sebastian Miotk