Yanki Twizzy
Yanki Twizzy

Reputation: 8001

Using variables defined in an included file (included by another file) in the including file in php

Let's say I have three files that have the following code

File 1: firstfile.php

 
    $dbhost = "localhost";
    $dbname = "mydbname";
    $dbuser = "myuserid";
    $dbpass = "mypass";
    $dblink = mysql_connect($dbhost, $dbuser, $dbpass);
    mysql_select_db($dbname,$dblink);

    $errorlogs = "c:/wamp/www/smstrunk/tmp/weblogs.log";
    $displaycount = 2;
    $querylimit = "LIMIT $displaycount";
    $maximportfilesize = 2097152;

File 2: secondfile.php

include("firstfile.php");

//Returns the GET/POST parameter value
    function getPassedParam($param){

        if(isset($_POST[$param])) $value = $_POST[$param];
        elseif(isset($_GET[$param])) $value = $_GET[$param];
        else $value = "";

        return $value;
    }
//##########################################

// DB Functions
    function selectSQL($qry, $dblink){

        global $errorlogs;

        $result = mysql_query($qry, $dblink);
        if (!$result) {
            log2File("Invalid query: " . mysql_error());
            return false;
        }
        log2File($qry);

        return $result;
    }

    function updateSQL($qry, $dblink){

        $result = mysql_query($qry, $dblink);
        if (!$result) {
            log2File("Invalid query: " . mysql_error());
            return false;
        }
        log2File($qry);

        return mysql_affected_rows($dblink);
    }

    function insertSQL($qry, $dblink){
            echo $displaycount;
        $result = mysql_query($qry, $dblink);
        if (!$result) {
            log2File("Invalid query: " . mysql_error());
            return false;
        }
        log2File($qry);

        return mysql_insert_id();

    }
//##########################################

//Mailing Functions
    function auto_reg_mail($to, $message){

        $subject = 'Registration Confirmation';
        $headers = 'From: [email protected]' . "\r\n" . 'X-Mailer: PHP/' . phpversion();

        mail($to, $subject, $message, $headers);
    }
//##########################################


//Logging Function
    function log2File($message, $file=NULL){

        global $errorlogs;
        if($file == NULL)$file = $errorlogs;

        $msg = date("Y-m-d H:m:s") . " [" . $_SERVER['PHP_SELF'] . "] $message \n";
        error_log($msg, 3, $file);
    }
//##########################################


// Application Specific Functions
//  1. Get customer id from msisdn
    function getCustID($msisdn, $lowner, $dblink, $alias = ""){

        //Check if msisdn already exists for this user
        $sql = sprintf("SELECT custid, mstatus FROM msisdn WHERE number = '%s' AND owner = '$lowner'", mysql_real_escape_string($msisdn));
        $rsmsisdn = selectSQL($sql, $dblink);

        if($rwmsisdn = mysql_fetch_assoc($rsmsisdn)){
            $custID = $rwmsisdn['custid'];
            $mstatus = $rwmsisdn['mstatus'];
            $sql = sprintf("UPDATE msisdn SET mstatus = 0, alias = '%s' WHERE custID = '$custID'", mysql_real_escape_string($alias));
            updateSQL($sql, $dblink);
        }else{
        // if not insert number
            $sql = sprintf("INSERT INTO msisdn (number, owner, alias) VALUES ('%s', '$lowner', '%s')", $msisdn, mysql_real_escape_string($alias));
            $custID = insertSQL($sql, $dblink);
        }
        return $custID;
    }

?>

File 3: third.php

include ("secondfile.php");

echo $dblink;

Why is $dblink giving me undefined variable when I have already defined it in firstfile.php? Does the php compiler/interpreter allow limited levels of include

Upvotes: 0

Views: 196

Answers (1)

Pekka
Pekka

Reputation: 449783

[I] am not calling includes through a file system path

include() fetches a file, and then executes the PHP source code in it.

If you are using a http:// path, every include will get interpreted separately, in its own parser instance. That way, variables defined in one include will not make it to the other.

You need to use filesystem paths (or make your http includes emit source code instead of executing it).

Upvotes: 1

Related Questions