php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #21663 Functions closing mysql resources
Submitted: 2003-01-15 10:15 UTC Modified: 2003-01-15 12:48 UTC
From: darylm at magiasoftware dot com Assigned:
Status: Not a bug Package: MySQL related
PHP Version: 4.2.3 OS: Unix (FreeBSD)
Private report: No CVE-ID: None
 [2003-01-15 10:15 UTC] darylm at magiasoftware dot com
There are two databases involved:
database1 is connected to in the main body of the page
database2 is connected to in a function

========================================================
function foo()
{
   mysql_connect(Same Arguements)
   db1_connection = select_db (db1)
   do something
   close db1_connection
}

mysql_connect(Same Arguements)
db2_connection = select_db(db2)

do something with db1

foo()

do something with db1 //**** This will fail
======================================================

The second "do something with db1 will fail with a "not a valid result resource" error.  I percieve of two possible reasons for this:

1.  mysql_close(db1_connection) is closing all the links

or, most likely

2.  Since mysql_connect is called with the same arguements it returns the same resource id (I know this) then, when I close db1_connection it closes  that resource.

So, either way we have a variable scope issue.  According to what I understand about the scope rules of php the functions should have no knowledge of my connection to db2 unless I declare it as global in the function foo.  But, it does anyway.

As an aside, I used to be able to globally include a resource id into a function, leave it open and return from the function.  Now, it appears that when the function exits it disposes of the "global" resource in its clean up proceedures.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-01-15 11:33 UTC] georg@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

See http://www.php.net/manual/en/function.mysql-connect.php
 [2003-01-15 11:47 UTC] darylm at magiasoftware dot com
Please read my message in its entirety.  I know mysql-connect returns the same result resource if passed the same parameters.  That is not the question.  The question is one of variable scope!  Should the call to mysql_connect from within the function even be able to see that a resource id exists in another part of the program that is out of scope for the executing function.  That is the question!  The answer should be "no", it is the principle of least priveledge here, the function should not know about the out of scope resource id.

I have been writing php code for some time and this used to work correctly.  It got broken in one of the more recent releases and either:

no one is willing to take the time to understand what I am talking about

 or, 

it was designed this way on purpose and no one knows why.

If this is truly not a bug than php scope rules appear to be, well, poorly defined and, that is an issue the language will have to deal with at some point in time.
 [2003-01-15 12:48 UTC] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

MySQL connections are stored inside an internal stucture and are not affected by scope. As of PHP 4.2.0 you can force mysql_connect to open a new connection rather then attempt to re-use an existing one by specifying TRUE as the 4th parameter to the function.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 11 01:01:31 2024 UTC