2017-02-13 89 views
0

使用下面的代碼總是連接到數據庫的某些cron函數突然開始失敗,這有什麼問題?錯誤:mssql_connect():無法連接到服務器:

看來它不能連接到數據庫。憑證是好的,他們沒有改變。 TestConnect是一個被調用的類,然後我想連接到我的數據庫。我這樣使用它:

$test = new \TestConnect(true); 

現在,類文件被發現,它的工作據我所知。

class TestConnect 
{ 

private $_server = 'test'; 
private $_username = 'test'; 
private $_password = 'password'; 
private $_database = 'database'; 
private $_query = null; 
private $_handle = null; 
private $_handle_db = null; 
private $_result = null; 

public function __construct($connect = null, $query = null) 
{ 
    if ($connect) 
    { 
     $this->connect(); 
    } 

    if ($query) 
    { 
     $this->query($query); 
    } 
} 

public function connect() 
{ 
    ini_set('memory_limit','1024M'); 
    putenv("FREETDSCONF=/etc/freetds.conf"); 

    if ($this->_handle) 
    { 
     return $this->_handle; 
    } 
    else 
    { 
     if (! $this->_handle = mssql_connect($this->_server, $this->_username, $this->_password)) 
     { 
      throw new Exception($this->get_error_message()); 
     } 

     if (! $this->_handle_db = mssql_select_db($this->_database, $this->_handle)) 
     { 
      throw new Exception($this->get_error_message()); 
     } 
    } 
} 
} 

現在最有趣的事情是,如果我把$ test = new \ TestConnect(true);變成某種功能,並從另一個功能調用它,它似乎工作。但是如果我打電話給$ test = new \ TestConnect(true);從相同的功能 - 它不工作。

它的工作是這樣的:

private function getDatabase(){ 
    $test = new \TestConnect(true); 
    $test->query("SELECT id as id FROM table"); 
    return $test->fetch_assoc($trim = true); 
} 

我這樣調用函數:

$users = self::getDatabase(); 

這樣的連接工程,我得到的所有數據。但是如果我使用沒有任何功能的代碼 - 它不起作用。這怎麼可能?

+1

您最近升級到PHP7嗎? – argoden

+0

不,版本是5.4。 – The50

+0

發佈更新,更多信息。 – The50

回答

0

發現斷開連接功能導致了問題。在所有函數和數據查詢之後,將代碼更改爲僅從數據庫斷開連接一次。

相關問題