Jump to content

Recommended Posts

I need to access to different databases within one script. How can I save the resource identifier from the first connection into a variable?

Basically what i need to do is:

Establish a global connection

          Save link identifier

          Establish a second connection

          Close second connection

          Restore original link identifier so that mysql_query(select blah) without specific link identifier could work using the original global connection.

 

Thank you very much. Please let me know if you need specifics to understand the question better.

Link to comment
https://forums.phpfreaks.com/topic/38701-solved-mysql-link-identifier/
Share on other sites

The second parameter to the mysql_query function is the "link identifier".  This means that you can have 1000 db connections open at a time and select any one of them to perform any query when ever you want...

 

$link1 = mysql_connect(..., ..., ...);
mysql_select_db($database_name, $link1);

$link2 = mysql_connect(..., ..., ...);
mysql_select_db($database_name_two, $link2);

$result = mysql_query("SELECT * FROM table2", $link1) or die(mysql_error());

$result = mysql_query("SELECT * FROM table", $link2) or die(mysql_error());

 

If you are connecting to the same database host, but are using to different databases, you can simply call mysql_select_db again to change the "default" database that the connection is using.

The second parameter to the mysql_query function is the "link identifier".  This means that you can have 1000 db connections open at a time and select any one of them to perform any query when ever you want...

 

$link1 = mysql_connect(..., ..., ...);
mysql_select_db($database_name, $link1);

$link2 = mysql_connect(..., ..., ...);
mysql_select_db($database_name_two, $link2);

$result = mysql_query("SELECT * FROM table2", $link1) or die(mysql_error());

$result = mysql_query("SELECT * FROM table", $link2) or die(mysql_error());

 

If you are connecting to the same database host, but are using to different databases, you can simply call mysql_select_db again to change the "default" database that the connection is using.

 

I'm sorry for not clarifying my question because you are the second person who tells me that. I know that second parameter is the link identifier, but there is a whole lot of code written without that second parameter. So what i need to do is to set the FIRST link identifier as DEFAULT again after I make, use, and close the SECOND connection. So what I'm trying to do is to save the first link identifier, make the second connection, reset the first link identifier as default.

To be even more specific, I'm looking for something like $link = currentLinkID();

The FIRST $link1 = mysql_connect() is called outside of the script I'm writing within a library that handles all DB connections so I'm not sure how php stores that first link identifer ($link1), but I need to get it to restore it back to default after I make the second connection.

 

And no, it is not the same db, so i can't use mysql_select_db, different servers.

 

Thank you for reply, i hope i made my problem clear.

There is a second, optional parameter to mysql_query where you can specify a resource link. So you can do this:

 

mysql_query("SELECT * FROM user", $db1);

mysql_query("SELECT * FROM tableOnDb2", $db2);

 

Are you kidding? I already said twice that I know, but cannot use that parameter. Thanks for reply

I already figured out my problem. I moved from one server to another and external db on this server was on the same server and i was accessing it with the same credentials unlike on the other server. hitman6003 your comment was the most helpful.

 

Thanks to everyone who responded.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.