daq Posted February 15, 2007 Share Posted February 15, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/38701-solved-mysql-link-identifier/ Share on other sites More sharing options...
trq Posted February 15, 2007 Share Posted February 15, 2007 <?php $db1 = mysql_connect('foo'); $db2 = mysql_connect('bar'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/38701-solved-mysql-link-identifier/#findComment-185899 Share on other sites More sharing options...
papaface Posted February 15, 2007 Share Posted February 15, 2007 $con1 = mysql_connect("host1","username1","pw1") or die (mysql_error()); $con2 = mysql_connect("host2","username2","pw2") or die (mysql_error()); mysql_close($con2); mysql_select_db("database",$con1); Quote Link to comment https://forums.phpfreaks.com/topic/38701-solved-mysql-link-identifier/#findComment-185900 Share on other sites More sharing options...
trq Posted February 15, 2007 Share Posted February 15, 2007 Sorry, reread you post. mysql_query() will default to the last opened connection, so if you need it to default back to $db1, you will need reconnect to it. Quote Link to comment https://forums.phpfreaks.com/topic/38701-solved-mysql-link-identifier/#findComment-185901 Share on other sites More sharing options...
daq Posted February 16, 2007 Author Share Posted February 16, 2007 Problem is, i don't know (may not have access to) the place where the first global connection is established. I know mysql_connect returns the link. How can I get the link identifier after mysql_connect has been run? Quote Link to comment https://forums.phpfreaks.com/topic/38701-solved-mysql-link-identifier/#findComment-185929 Share on other sites More sharing options...
daq Posted February 16, 2007 Author Share Posted February 16, 2007 /papaface I need to restore the first link identifier as default so I DON'T have to use specific identifier in the query/select_db call. Thanks for reply though. Quote Link to comment https://forums.phpfreaks.com/topic/38701-solved-mysql-link-identifier/#findComment-185930 Share on other sites More sharing options...
hitman6003 Posted February 16, 2007 Share Posted February 16, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/38701-solved-mysql-link-identifier/#findComment-185970 Share on other sites More sharing options...
daq Posted February 16, 2007 Author Share Posted February 16, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/38701-solved-mysql-link-identifier/#findComment-186579 Share on other sites More sharing options...
superuser2 Posted February 16, 2007 Share Posted February 16, 2007 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); Quote Link to comment https://forums.phpfreaks.com/topic/38701-solved-mysql-link-identifier/#findComment-186686 Share on other sites More sharing options...
daq Posted February 16, 2007 Author Share Posted February 16, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/38701-solved-mysql-link-identifier/#findComment-186704 Share on other sites More sharing options...
daq Posted February 16, 2007 Author Share Posted February 16, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/38701-solved-mysql-link-identifier/#findComment-186707 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.