2017-02-13 26 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

发现断开连接功能导致了问题。在所有函数和数据查询之后,将代码更改为仅从数据库断开连接一次。

相关问题