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 ); 
     }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 vscode的问题提问
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM