2016-12-16 40 views
0

我有一個擁有所有其他數據庫名稱的主數據庫,當用戶登錄時,我在此主數據庫上找到了appropraite數據庫名稱,然後使用該名稱將用戶連接到系統。我做的是,獲取數據庫名後,我將它保存爲一個會話,並將該會話傳遞給我的其他數據庫連接的數據庫名稱。這就是我如何將用戶連接到系統在旅途中更改數據庫名稱php

class DataDB extends mysqli { 

// single instance of self shared among all instances 
private static $instance = null; 
// db connection config vars 
private $dbName = $_SESSION['dname']; 
private $dbHost = "localhost"; 

//This method must be static, and must return an instance of the object if the object 
//does not already exist. 
public static function getInstance() { 
    if (!self::$instance instanceof self) { 
     self::$instance = new self; 
    } 
    return self::$instance; 
} 

到目前爲止其拒絕工作正在此錯誤解析錯誤:語法錯誤,意想不到的「$ _SESSION」(T_VARIABLE)。也有沒有更好的方式來做到這一點沒有會話

回答

0

改變你在你們班的__construct()方法設置其分配私有財產的方式。

雖然我不建議這樣做,使用配置文件,您可以使用此Symphony component雖然。這裏有一個example

又一個快速教程使用composer可能會幫助你

編輯:

private function __construct() { 
    $this->dbName = $_SESSION['dname']; 
    parent::__construct($this->dbHost, $this->user, $this->pass, $this->dbName); //or pass it directly 
} 
+0

感謝了很多,看它現在@ka_lin – hamobi

+0

檢查我的編輯的版本,雖然,我misstake –

+0

這我怎麼分配它'父:: __結構($這個 - > DBHOST,$這個 - >用戶,$這個 - >通,$這個 - >數據庫名);' – hamobi