开发者

Make a temporary DB connection without losing the original default connection

开发者 https://www.devze.com 2023-02-25 21:23 出处:网络
I have a very large and unstructured script. At some point in the script, a DB connection is made. Some time after that point I need to make a new connection to another DB, run a query then disconne

I have a very large and unstructured script.

At some point in the script, a DB connection is made.

Some time after that point I need to make a new connection to another DB, run a query then disconnect.

After I've done this, I need all subsequent mysql_query() calls to use the original DB link.

I know I can capture the link identifier from the first mysql_connect(), and use that in all relevant mysql_query() calls, but I don't want to modify any more code than I have to. I'm looking to do something like this:

//... loads of code ...

mysql_connect("original connection");
mysql_query(...); // don't want to modify these lines

//... loads more code ...

$link = get_current_mysql_link_identifier(); // imaginary function
$new_link = mysql_connect("my new connection");
mysql_query(...); // uses new connection
mysql_close($new_link);
reinstate_old_link($link); // imaginary function

//... loads more code ...

mysql_query(...) // line remains unchanged. uses original DB link

Without those imagi开发者_如何学JAVAnary function, once the second connection is closed, mysql_query() does not default to the original connection; it continues to try and use the more recent, closed connection.


mysql_connect() returns a reference to the connection, which you can then provide on other statements to indicate the connection to be used. I believe you also have to specify the new_link argument to create a new connection.

0

精彩评论

暂无评论...
验证码 换一张
取 消