2013-12-16 45 views
0

我有以下設置:Apache/php 5.3/pdo與odbc與安裝的Microsoft SQL Server ODBC驅動程序1.0在服務器上的Linux。試圖執行一個語句時Php pdo unixOBDC到sqlserver 2008:字符串數據,長度不匹配時執行準備好stmt

我的腳本上升的錯誤有以下堆棧跟蹤:

(UTC) 2013-12-16 12:07:40: Thrown exception log -> 
'Error message: SQLSTATE[22026]: String data, length mismatch: 0 [Microsoft][SQL Server Native Client 11.0]String data, length mismatch (SQLExecute[0] at /tmp/pear/temp/PDO_ODBC/odbc_stmt.c:133) 
Arisen in Core_Db->select(array (
     0 => 
     'SELECT * 
      FROM TMS.dbo.TEST_user 
      WHERE email = ? AND status_id = ?', 
     1 => 
     array (
      0 => '[email protected]', 
      1 => 2 
     ), 
)) 

爲了測試我使用PHP 5.3與PDO SQLSRV並沒有什麼窗口了問題存在。

連接代碼

 // for unixODBC (production) 
     if (DB_DRIVER == 'odbc') { 
      $this->_link = new PDO(
       "odbc:DRIVER={SQL Server Native Client 11.0};SERVER=" . DB_HOST . ";" 
       . "PORT=" . DB_PORT . ";DATABASE=" . DB_NAME . ";PROTOCOL=TCPIP;UID=" . DB_LOGIN . ";" 
       . "PWD=" . DB_PASSWD . ";" 
      ); 
     // for sqlsrv (development) 
     } else { 
      $this->_link = new PDO(
       "sqlsrv:Server=" . DB_HOST . "," . DB_PORT . ";Database=" . DB_NAME, 
       DB_LOGIN, 
       DB_PASSWD 
      ); 
     } 
     $this->_link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

有關於這個問題的一些建議:ODBC and SQL Server 2008: Can't use prepared statements?。但我無法檢查它。當我嘗試在生產中添加屬性時,腳本出現以下錯誤:

(UTC) 2013-12-16 13:19:44: SQLSTATE[IM001]: Driver does not support this function: driver does not support setting attributes 

有誰知道如何解決此問題?

已更新2013年12月18日

// unixODBC connection 
$this->_link = new PDO(
    "odbc:DRIVER={SQL Server Native Client 11.0};SERVER=xxx.xxx.xxx.xxx;" 
    . "PORT=1433;DATABASE=TMS;PROTOCOL=TCPIP;", 
    DB_LOGIN, 
    DB_PASSWD, 
    array(
     PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // this attr is recognized by ODBC 
     PDO::ATTR_EMULATE_PREPARES => false // but if this is added ODBC throws PDOException with message "driver does not support setting attributes" 
    ) 
); 

同時使用準備好的聲明中字符串的從ODBC轉移到客戶端導致字符串的腐敗。所以我只能猜測這件事是在PDO :: ATTR_EMULATE_PREPARES。

回答

0

錯誤

(UTC) 2013-12-16 13:19:44: SQLSTATE[IM001]: Driver does not support this function: driver does not support setting attributes

告訴你這行:

$this->_link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

是無效的。相反,這樣做:

$driver_options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, 
         PDO::ATTR_EMULATE_PREPARES => FALSE); 

if (DB_DRIVER == 'odbc'){ // for unixODBC (production) 
    $dsn = 'odbc:DRIVER={SQL Server Native Client 11.0};SERVER='. DB_HOST .';PORT='. DB_PORT .';DATABASE='. DB_NAME .';PROTOCOL=TCPIP;'; 
    $this->_link = new PDO($dsn, DB_LOGIN, DB_PASSWD, $driver_options); 
}else{ // for sqlsrv (development) 
    $this->_link = new PDO(
     "sqlsrv:Server=" . DB_HOST . "," . DB_PORT . ";Database=" . DB_NAME, 
     DB_LOGIN, 
     DB_PASSWD 
    ); 
} 
+0

不,當我申請$ pdo->的setAttribute(PDO :: ATTR_EMULATE_PREPARES,FALSE)只發生錯誤; – Eugeny

+0

@Eugeny - 我已經更新了我的答案。您不能使用'setAttribute()'函數,您必須在創建PDO對象時設置該選項。 –

+0

不幸的是,同樣的錯誤。它不允許設置除PDO :: ATTR_ERRMODE之外的任何屬性,即使在創建PDO時也是如此。 – Eugeny