简体   繁体   English

PHP-PDO中的“连接已重置”

[英]“The connection was reset” in PHP-PDO

I am trying to access SQL Window Server from Linux (ubuntu 12.04) server by PHP PDO extension, but showing me "The connection was reset" from the browser. 我试图通过PHP PDO扩展名从Linux(ubuntu 12.04)服务器访问SQL Window Server,但从浏览器中看到“连接已重置”。

Code is - 代码是-

 try {
        self::$instance = new PDO('odbc:Driver=FreeTDS; Server=192.168.0.21; Port=1433; Database=MSSQLTips; UID=XXXX; PWD=XXXXX');
        self::$instance->setAttribute(PDO::ATTR_PERSISTENT, true);
        self::$instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);       

      } catch (PDOException $exception) {
            trigger_error($error->getMessage());
      }

And the access code is 访问代码是

$query = 'SELECT * FROM tblEmployee where Employee_Id = ?';       
$sth = $this->db->prepare($query);               
$sth->execute(array('1557'));
$result = $sth->fetch();
echo "<pre>"; print_r($result); die;

This is a known bug in PHP. 这是PHP中的一个已知错误。 Refer to bug report 64483 for more information. 有关更多信息,请参考错误报告64483

As an alternative to pdo_odbc you can use the pdo_dblib driver to access a MS SQL Server from PHP on Linux. 作为pdo_odbc的替代方法,可以使用pdo_dblib驱动程序从Linux上的PHP访问MS SQL Server。 Refer to the PHP documentation on pdo_dblib 请参阅pdo_dblib上PHP文档

I have adjusted your code to use dblib. 我已经调整了您的代码以使用dblib。

try { 
    self::$instance = new PDO ("dblib:host=192.168.0.21:1433;dbname=MSSQLTips","XXXX","XXXXX");
    self::$instance->setAttribute(PDO::ATTR_PERSISTENT, true);
    self::$instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);       

  } catch (PDOException $ex) {
      $msg = htmlentities($ex->getMessage());    
      trigger_error($msg);
  }

And then: 接着:

$query = 'SELECT * FROM tblEmployee where Employee_Id = ?';       
$sth = $this->db->prepare($query);
$sth->bindValue(1, "1557", PDO::PARAM_INT);               
$sth->execute();
$result = $sth->fetchAll();
if($result === false) {
    throw new Exception("Unable to fetch result.");
}

try {
    echo "<pre>"; 
    print_r($result);
    echo "</pre>";
} catch (Exception $ex) {
    $msg = htmlentities($ex->getMessage());
    trigger_error($msg,E_WARNING);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM