php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #49400 New Functions for Getting and Setting the Default Connection Handle
Submitted: 2009-08-28 15:11 UTC Modified: 2011-01-06 15:38 UTC
Votes:15
Avg. Score:4.3 ± 1.2
Reproduced:13 of 14 (92.9%)
Same Version:4 (30.8%)
Same OS:2 (15.4%)
From: rahlentertainment at gmail dot com Assigned: mysql (profile)
Status: Wont fix Package: MySQL related
PHP Version: 5.2.10 OS: Ubuntu 9.04
Private report: No CVE-ID: None
 [2009-08-28 15:11 UTC] 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.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-12-29 13:08 UTC] jani@php.net
-Package: Feature/Change Request +Package: MySQL related
 [2011-01-06 15:38 UTC] uw@php.net
-Status: Open +Status: Wont fix -Assigned To: +Assigned To: mysql
 [2011-01-06 15:38 UTC] uw@php.net
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...
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 05:01:30 2024 UTC