Jump to content

Two MySQL connections, only second one works.


Jerred121

Recommended Posts

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!

Link to comment
Share on other sites

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){

 

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

 

 

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.