2013-12-16 44 views
0

我被小consfused建立,我真的需要對代碼:)
這是我翻譯功能一些新鮮的觀點:到服務器的鏈接無法爲不明原因

function __($string) { 
    isset($_SESSION['lang']) ? $lang = $_SESSION['lang'] : $lang = DEFAULT_LANG; 
    $oDb = new Model_Database('translations'); 
    $aLang = $oDb->getById($string); 
    return $aLang[$lang]; 
} 

這是Model_Database,它使用:

class Model_Database { 

    static private $connection = ''; 
    private $aConfig; 
    private $sTable; 

    public function __construct($sTable = '') { 
     $this->aConfig = Core_Config::GetConfigArray('database'); 
     if(empty(self::$connection)) { 
      self::$connection = mysql_connect($this->aConfig['host'], $this->aConfig['user'], $this->aConfig['pass']); 
     } 
     mysql_select_db($this->aConfig['db'], self::$connection); 
     $this->sTable = TABLE_PREFIX . $sTable; 
    } 

    public function getById($id) { 
     $query = 'SELECT * FROM `' . $this->sTable . '` WHERE `id` = "' . $id . '"'; 
     $result = mysql_fetch_array(mysql_query($query)); 
     return $result; 
    } 

    public function getByAny($column, $value) { 
     $query = 'SELECT * FROM `' . $this->sTable . '` WHERE `' . $column . '` = "' . $value .'"'; 
     $result = mysql_fetch_array(mysql_query($query)); 
     return $result; 
    } 

    public function __destruct() { 
     if(gettype(self::$connection) == "resource") { 
      mysql_close(self::$connection); 
     } 
     self::$connection = ''; 
    } 
} 

這是Model_Login也使用大tabase:

class Model_Login { 

    private $oDatabase; 

    public function __construct() { 
     $this->oDatabase = new Model_Database('users'); 
    } 

    public function login($login, $password) { 
     $aUser = $this->oDatabase->getByAny('login', $login); 
     if(md5($password) == $aUser['password']) { 
      $_SESSION['login'] = $login; 
      $_SESSION['password'] = md5($password); 
      return true; 
     } 
     else 
      return false; 

    } 
} 

的問題是,當我嘗試使用翻譯功能在文件中負責登錄,我得到錯誤Access denied for user 'ODBC'@'localhost'A link to the server could not be established
起初我遇到了關閉和打開新連接和登錄工作但翻譯沒有問題。所以我找到了將連接記憶爲靜態變量的解決方案。翻譯開始工作,但登錄已停止。
任何建議?
編輯:錯誤顯示出來,當我點擊「登錄」,這創造了新的Model_Login並調用方法登錄()
編輯2:如果您需要任何更多的代碼來重現錯誤,我可以連共享整個項目,就像你看到的那樣,它處於早期建設階段,只是做基本的功能。當然,如果需要,我可以只發一些更多的代碼,只需指定你需要什麼來幫助我:)

回答

0

我在等待這種方式時提出瞭解決方案:
1.我將此方法添加到數據庫類

public function checkConnection() { 
     if(gettype(self::$connection) != "resource") { 
      self::$connection = mysql_connect($this->aConfig['host'], $this->aConfig['user'], $this->aConfig['pass']); 
      mysql_select_db($this->aConfig['db'], self::$connection); 
     } 

    } 

2.我在第一行添加了這個方法。

解決方案的作品,但不是很「漂亮」,你知道有更好的方法來做到這一點嗎?

編輯:我找到了更簡單的解決方案。
而不是checkConnection()我做closeConnection()方法並增加它在我的的index.php結束(整個網站可以作爲一個應用程序,一切順利的通過的index.php)。還刪除了數據庫類中的析構函數。這裏的代碼,希望有人會對我的錯誤有所瞭解:):

public static function closeConnection() { 
    if(gettype(self::$connection) == "resource") { 
     mysql_close(self::$connection); 
     self::$connection = ''; 
    } 
} 
相關問題