w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
PHP Scope Issue Between Included File Variable, Global Function, And Classes
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
); 
 }




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.