Jerred121 Posted February 4, 2011 Share Posted February 4, 2011 I've looked every where, probably googled every variation of my question I could come up with and I've tried all of the suggestions... I'm trying to connect to two different DBs in two different locations ($local and $remote) and only the second one works. Here is a sample of my code ("..." = hidden): //-------------Local DB Connection: $local = mysql_connect("localhost","root","..."); if (!$local) { die('Could not connect: ' . mysql_error()); } $sel1 = mysql_select_db("new", $local); //-------------Remote DB Connection: $remote = mysql_connect("...","...","...",true); if (!$remote) { die('Could not connect: ' . mysql_error()); } $table = "..."; //---------function selecting from local: function fncGrabNemsis($ele,$val){ mysql_select_db("new", $local); $result = mysql_query("SELECT * FROM new.tblvalues WHERE fldelement='$ele' AND fldcode='$val'",$local); $tmprow = mysql_fetch_array($result); return (isset($tmprow['fldvariable'])?$tmprow['fldvariable']:$val); } //----------Select run from Remote: mysql_select_db("ImdxTest", $remote); $result = mysql_query("SELECT * FROM ImdxTest.$table WHERE ClientID = ... AND IncidentNum = '$fldINCID'", $remote) or die(mysql_error()); $row = mysql_fetch_array($result); I've tried moving the mysql_select_db() function calls everywhere you can think of and just about everything else... What happens is, I get php errors saying that $local is not defined or that the mysql function that are trying to use the $local connection are expecting parameters to be resources!? I know for a fact that both connections work because individually they both work. Only the second connection ($remote) works... Thanks a lot for any suggestions! Quote Link to comment https://forums.phpfreaks.com/topic/226719-two-mysql-connections-only-second-one-works/ Share on other sites More sharing options...
fortnox007 Posted February 4, 2011 Share Posted February 4, 2011 I tried to connect to a remote database earlier, and it seemed the host didn't allow external database connection. Maybe look if that is even allowed. Quote Link to comment https://forums.phpfreaks.com/topic/226719-two-mysql-connections-only-second-one-works/#findComment-1170021 Share on other sites More sharing options...
PFMaBiSmAd Posted February 4, 2011 Share Posted February 4, 2011 Everything inside of a function definition is 'local' to that function. This is so you can write functions that do whatever is necessary to accomplish the task you have defined for that function without needing to keep track of the variables you use inside that function. You would need to add a third parameter to your function definition to serve as the mysql $link and then pass the mysql connection link variable into the function when you call your function - function fncGrabNemsis($ele,$val,$local){ Quote Link to comment https://forums.phpfreaks.com/topic/226719-two-mysql-connections-only-second-one-works/#findComment-1170026 Share on other sites More sharing options...
Jerred121 Posted February 4, 2011 Author Share Posted February 4, 2011 ah, that makes sense! Thank you for the suggestion. I actually just got it working - I just selected the DB right after I defined the connection and swapped the order that the connection were being made - It doesn't make sense to me but it worked... Could a issue result from this later down the road? Why does do they both work when in this particular order but only one works in the order I had it before? Quote Link to comment https://forums.phpfreaks.com/topic/226719-two-mysql-connections-only-second-one-works/#findComment-1170044 Share on other sites More sharing options...
PFMaBiSmAd Posted February 4, 2011 Share Posted February 4, 2011 When you don't supply a mysql connection link in the mysql_xxxxx($a,$link) statements, they use the last link that was created. If you have code that requires more than one mysql connection and you want that code to ALWAYS work, with the least amount of effort on your part, you would need to use the correct connection $link in the code that needs it. If that code happens to be inside of a function definition, simply pass the connection link into the function as a parameter when you make the call to that function. Quote Link to comment https://forums.phpfreaks.com/topic/226719-two-mysql-connections-only-second-one-works/#findComment-1170050 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.