dqpfkzu360216 2014-07-21 13:44
浏览 60

包含文件变量,全局函数和类之间的PHP范围问题

I'm having a problem figuring out why a function within a class isn't properly using the global function for connecting to a database. It can access the function fine and it will set up a connection string, but it connects to the wrong database and I think it has to do with scoping, but not sure how to remedy it.

It's set up something like this

include "variableScript.php"; //contains variable set by another script

//other code

function connectDB(){
   global $scriptVar;
   $db;

   if(strcmp($scriptVar, 'DB1') == 0){
      $db = 'DB1';
   }
   else{
      $db = 'DB2';
   }

   //DB connection code here, user/pass come elsewhere.
}

//other code

class A{ //this class is called elsewhere
   // class variables here, one being a database connector
   // class constructor

   public function run(){
      $this->dbConn = connectDB();
      //other code in run function
   }

   // other class function
}

Now, I know for sure that $scriptVar is set correctly and that the database selected is correct when it's another script's function (not in a class) using the connectDB function so I think it has to deal with the scoping and the class. As I said, I think it's a scoping issue. When the global function is called from inside the class, I don't think it knows what $scriptVar is. I'm not sure if it's trying to find $scriptVar in the class or elsewhere. When I tried putting global $scriptVar inside class A's function that calls the other, it didn't work out for me.

Anyone know what could be up?

  • 写回答

1条回答 默认 最新

  • duanchuang3182 2014-07-21 13:56
    关注
    function connectDB(){
       global $scriptVar;
       $db="";
    
       if(strcmp($scriptVar, 'DB1') == 0){
          $db = 'DB1';
       }
       else{
          $db = 'DB2';
       }
    
       //DB connection code here, user/pass come elsewhere.
    }
    

    i think it will be helpful to you if it will not work you can use this code

    class R_DB
    {   
        public $host     = "localhost";
        public $database = "dbname"; 
        public $username = "root"; 
        public $password = ""; 
        public $conId    = 0;  
        public $row;          
    
     //call connection method upon constructing 
     public function __construct(){
      $this->dbConnect(); 
     }
    
     //connection to the database
     public function dbConnect() 
        { 
            if( 0 == $this->conId ) 
                $this->conId=mysql_connect( $this->host, $this->username, $this->password ); 
            if( !$this->conId ) 
       $this->stopExec( "Trying to connect.... Result: failed" ); 
            if( !mysql_query( sprintf( "use %s", $this->database ), $this->conId ) ) 
                $this->stopExec( "cannot use database ".$this->database ); 
     } 
    }
    

    can use optional db here using

    when

    public function dbConnect($host, $dbuser, $dbpass, $dbname) 
        { 
            if( 0 == $this->conId ) 
                $this->conId=mysqli_connect( $dbhost, $dbuser, $dbpass, $dbname ); 
            if( !$this->conId ) 
       $this->stopExec( "Trying to connect.... Result: failed" ); 
            if( !mysql_query( sprintf( "use %s", $dbname ), $this->conId ) ) 
                $this->stopExec( "cannot use database ".$this->database ); 
     }
    
    评论

报告相同问题?