cgm225 Posted February 22, 2008 Share Posted February 22, 2008 I have a script that connects to one database (DB #1) for use throughout that script, which I close at the end of the entire script. However, within that script I have a function I use to connect to a second database (DB #2), which I grab some other information from. I end that function by closing that mysql connection. However, now I am getting "invalid MySQL resource" errors from the script after the point which I use that function. So I wanted to know, how do you deal with multiple MySQL database connections within one script, particularly in the setting I have outlined above? Thanks in advance! Quote Link to comment Share on other sites More sharing options...
mem0ri Posted February 22, 2008 Share Posted February 22, 2008 Personally, I wouldn't change the entire connection, only switch databases within the same connection. mysql_select_db() is the function... ...that way...with the function you can start it with mysql_select_db($x) and then go back to mysql_select_db($y) at the end... ...unless you're connecting to two completely different db servers? Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted February 22, 2008 Share Posted February 22, 2008 Make sure you provide the link resource whenever you call a mysql function which requires it. eg: $conn1 = mysql_connect('server1', 'userr1', 'pass1); # server1 $conn2 = mysql_connect('server2', 'userr2', 'pass2'); # server2 # select databases mysql_select_database('server1_db', $conn1); # server1 database mysql_select_database('server2_db', $conn2); # server2 database #query server $server1_qry = mysql_query('Select * from tbl_name', $conn1); # query server 1 database $server2_qry = mysql_query('Select * from tbl_name', $conn2); # query server 2 database #close connections mysql_close($conn1) # close server1 connection mysql_close($conn2) # close server2 connection Variables $conn1 and $conn2 contain the link resource, you'll notice these are passed as the second parameters to the above functions. This is how you deal with multiple connections to different servers/databases as the sametime. Note not all mysql functions require the link resource to be provided you should check the manual for which functions do. Quote Link to comment Share on other sites More sharing options...
cgm225 Posted February 22, 2008 Author Share Posted February 22, 2008 Thank you both so much! Quote Link to comment Share on other sites More sharing options...
Barand Posted February 22, 2008 Share Posted February 22, 2008 If they are on the same server you can just specify the db to use inside the query SELECT x, y, z FROM DB2.tablename or even mix'n'match SELECT a.z, b.y FROM DB1.tableA as a INNER JOIN DB2.tableB as b ON a.col = b.col Quote Link to comment Share on other sites More sharing options...
cgm225 Posted February 23, 2008 Author Share Posted February 23, 2008 Still having some issues... I have attached the script I am using to load images*. It works fine, except when I try to use a function (also attached*) to get data from a second database. When I try to use that function, called "permission_for" the MySQL query following it's use in the main script (the query used to generate the "Next" link) fails, outputting the following error message: "Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /web/example.com/includes/php/gallery/photos.inc.php on line 165" Could someone help me figure out why this is happening? Thank you all again! *I tried to use the [] code tags to include these items, but I was getting an error when trying to do so. I apologize for the inconvenience of the attachments. Quote Link to comment Share on other sites More sharing options...
cgm225 Posted February 24, 2008 Author Share Posted February 24, 2008 bump Quote Link to comment 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.