简体   繁体   中英

Functions does not recognize variable from a page loaded with require_once 'APPATH (…)'

Im trying to load a page with require_once on CodeIgniter but after it loads, if i want to call a variable from it, it will not recognize it. Why would this happens? makes no sense

file i want to load:

<?php

    $servidorodbc=file(APPPATH.'libraries/odbc.txt',FILE_IGNORE_NEW_LINES);
            $dsn=$servidorodbc['0'];
            $user=$servidorodbc['1'];
            $pwd=$servidorodbc['2'];


            if($pwd="''"){
                $pwd="";
            };

            $db = odbc_connect($dsn, $user, $pwd);
    ?>

Function where i want it to load:

     public function usuarioodbc($codigo,$nif){
            require_once(APPPATH.'libraries/odbc_conn.php');
                    $query = odbc_exec($db, 'select COUNT(*) as counter, Descricao,Codigo, N_Contrib from GP_Mn_Empregados where Codigo ='.$codigo.' AND N_Contrib="'.$nif.'" GROUP BY Descricao,Codigo, N_Contrib');
$resultado = odbc_fetch_array($query);

     if($resultado['counter'] == 1){
                return $resultado['Descricao'];
             }else{
                return false;
        }

    }

Error i get :

Message: Undefined variable: db

using require_once() will include the file only once during runtime.

example:

somefile.php :

$globalVar = 'somevalue';

runtime script:

function test1() {
    require_once('somefile.php');
    echo $globalVar; // somevalue
}

test1();

require_once('somefile.php');

echo $globalVar; // undefined

require('somefile.php');

// now $globalVar is in global scope
echo $globalVar; // somevalue

undefined the first time because the file it was included before when we called test1() so the file won't get included again and the $globalVar is available in the scope of test1()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM