php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #9107 Function with mysql_close($conn) closes top level scripts $conn
Submitted: 2001-02-05 04:06 UTC Modified: 2001-06-02 22:11 UTC
From: stormlrd at majestic-quartz dot com Assigned:
Status: Closed Package: MySQL related
PHP Version: 4.0.4pl1 OS: Linux Mandrake 7.0 / any really.
Private report: No CVE-ID: None
 [2001-02-05 04:06 UTC] stormlrd at majestic-quartz dot com
Please Also Refer to Bug Report 9049 :
I believe I am having the same problem. I think I have worked out why tho.

I have two files.
One has a function in it.
Both files require a connection to mysql database.
Both connections use the $conn as their pointer.
If you mysql_close($conn) within the function in the second file
then it closes the $conn in the script calling the function.
So you have to remove the mysql_close from your function
and even the $conn=mysql_connect from it and just pass the
$conn as a variable to the function :
eg
function foo($conn){
$result=mysql_db_query("test","select ...",$conn);
}
called from say test.html
$conn=mysql_connect("localhost","rootme","noway");
foo($conn);

was like this :

function foo()
{
$conn=mysql_connect("localhost","rootme","noway");
## query work etc..
mysql_close($conn);
return;
}

main script :
$conn=mysql_connect("localhost","rootme","noway");
#anything in here
#call function
$wotever=foo();
$result=mysql_db_query("database","query",$conn);

the last line brings up the error about the mysql link resource being wrong as the $conn was closed in the function but somehow closed it globally too. Which is a bit of a pain as this was ok before upgrading to Pl4

I didnt see anything in the change file about this :L So I can only assume
this is a bug.

If you need more info from me please let me know.. Thanks for all the good work guys :) 

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-02-05 04:08 UTC] stormlrd at majestic-quartz dot com
Sorry the PL4 was meant to be PHP4.04Pl1 :) 
 [2001-02-24 12:56 UTC] jmoore@php.net
This is due to the fact in this case PHP doesnt make two connections to MySQL but reuses the first one which you then close. Not sure if this should be fixed or is an acceptable side affect to an otherwise useful piece of behaviour (stops people opening silly amounts of connections to the same database if one is reuseable.)

James
 [2001-02-24 12:57 UTC] jmoore@php.net
This one is MySQL Related =)
 [2001-06-02 22:11 UTC] sniper@php.net
not a bug and documented behaviour.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 08:01:27 2024 UTC