Jump to content

How do you deal with need for MySQL connection inside function, outside function


cgm225

Recommended Posts

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!

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.