Edit report at http://bugs.php.net/bug.php?id=49400&edit=1

 ID:                 49400
 Updated by:         [email protected]
 Reported by:        rahlentertainment at gmail dot com
 Summary:            New Functions for Getting and Setting the Default
                     Connection Handle
-Status:             Open
+Status:             Wont fix
 Type:               Feature/Change Request
 Package:            MySQL related
 Operating System:   Ubuntu 9.04
 PHP Version:        5.2.10
-Assigned To:        
+Assigned To:        mysql
 Block user comment: N
 Private report:     N

 New Comment:

resource mysql_connect ([ string $server = ini_get("mysql.default_host")
[, string $username = ini_get("mysql.default_user") [, string $password
= ini_get("mysql.default_password") [, bool $new_link = false [, int
$client_flags = 0 ]]]]] )



mysql_connect returns a handle. Safe the handle, pass it to all
functions and everything is good. 



resource mysql_query ( string $query [, resource $link_identifier ] )



Using global magical handles floating around isn't really state of the
art development style...


Previous Comments:
------------------------------------------------------------------------
[2009-08-28 15:11:21] rahlentertainment at gmail dot com

Description:
------------
When you open a database connection with mysql_connect(), the function
returns a connection handle. Subsequent calls to mysql_query() assume
the last connection if no connection handle is specified. Unfortunately,
there doesn't seem to be any way to get this connection handle again,
without calling mysql_connect() a second time with the original
arguments.



There should be a way to get the connection handle again, without
calling mysql_connect(), using something like
mysql_get_last_connection() or similar.



Rational:

Consider a large PHP application. The creators call mysql_connect() at
the begining of their application, and, since they only use one
database, they don't save the connection handle, and let PHP
automatically use the last (and only) connection.



Another developer comes along, and writes a plug-in for the main
application. This plug-in requires a second database connection. When
the plug-in calls mysql_connect() to connect to it's database, the rest
of the original application breaks, because it's trying to use the
plug-in's database connection, because it was the last one opened.

Reproduce code:
---------------
// Start of a plug-in.

$OldDBH = mysql_get_last_connection(); // new function

$NewDBH = mysql_connect(...);

// some processing here...



// Plug-in has finished it's work

mysql_set_last_connection($OldDBH); // new function, tells php to use
the specified connection handle as the default for subsequent
mysql_query() calls which do not explicitly specify one.





// Plugin returns control to main app



// main app calls mysql_query() without specifying a dbh

mysql_query(); // this works, because $OldDBH was set back as the
default.



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



-- 
Edit this bug report at http://bugs.php.net/bug.php?id=49400&edit=1

Reply via email to