سؤال

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!

هل كانت مفيدة؟

المحلول

fncGrabNemsis needs the global variable $local:

    function fncGrabNemsis($ele,$val){
        global $local;
        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);
    }

This should help you understand better: http://php.net/manual/en/language.variables.scope.php

نصائح أخرى

$local is not available inside your function. Please read this: http://php.net/manual/en/language.variables.scope.php

[W]ithin user-defined functions a local function scope is introduced. Any variable used inside a function is by default limited to the local function scope.

In PHP, contrary to many other languages, outer scope is not avialble within functions. You must either pass the variable as an argument (preferred solution) or or use gloabal keyword, to import variable from global scope.

You are calling fncGrabNemsis from within a function. When you open a function, you get a clean slate in terms of variables. Variables that previously existed no longer do. $local is now undefined.

The easy way around this is to set $local to the global value:

function fncGrabNemsis($ele,$val){
    global $local; 
    mysql_select_db("new", $local);
    ...

Globals are probably not a good idea, however. You should rework your code so that they are unnecessary.

I see you are using $local inside a function, you should use global $local to reference the global scope $local variable from inside the function scope.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top