2013-04-25 187 views
1

我正在嘗試使用PDO連接到SQL Server數據庫。我一直在用它掙扎了一點點現在我得到我的網頁上這個嚴重的錯誤,我試圖連接到SQL Server如何在我的Windows 2008 Server 2008 R2上安裝pdo_sqlsrv?

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[IMSSP]: This extension requires the Microsoft SQL Server 2012 Native Client ODBC Driver to communicate with SQL Server. Access the following URL to download the Microsoft SQL Server 2012 Native Client ODBC driver for x86: http://go.microsoft.com/fwlink/?LinkId=163712 '

我從 http://www.microsoft.com/en-us/download/details.aspx?id=20098(SQLSRV30.EXE) 我下載了驅動器已將這些驅動程序放置在Server 2008 R2服務器上安裝php的ext目錄中。

我也有在php.ini文件的擴展名列表的末尾添加這兩條線

extension=php_pdo_sqlsrv_53_nts.dll 
extension=php_sqlsrv_53_nts.dll 

我使用PH PVersion 5.3.19 服務器API CGI/FastCGI的 線程安全禁用

我確實看到了pdo_sqlsrv,但我沒有看到客戶端API版本。

我還需要做些什麼才能夠使用PDO連接到具有SQL數據庫的遠程服務器?我需要在遠程服務器上安裝任何東西嗎?

這是我的PHP我管理部分

enter image description here

的截圖這是我的連接類

<?php 

class connection { 

    private $connString; 
    private $userName; 
    private $passCode; 
    private $server; 
    private $pdo; 
    private $errorMessage; 
    protected $lastQueryTime; 
    protected $lastQuery; 

    private $pdo_opt = array (
          PDO::ATTR_ERRMODE   => PDO::ERRMODE_EXCEPTION, 
          PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, 
          PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8" 
          ); 




    function __construct($dbName = DATABASE_NAME, $serverName = DATABASE_HOST){ 

     //sets credentials 
     $this->setConnectionCredentials($dbName, $serverName); 

     //start the connect 
     $this->startConnection(); 

    } 

    function startConnection(){ 


      $this->pdo = new PDO($this->connString, $this->userName, $this->passCode, $this->pdo_opt); 

      if(! $this->pdo){ 

       $this->errorMessage = 'Failed to connect to database. Please try to refresh this page in 1 minute. '; 
       $this->errorMessage .= 'However, if you continue to see this message please contact your system administrator.'; 
       echo $this->getError(); 
      } 
    } 


    //this will close the PDO connection 
    public function endConnection(){ 

     $this->pdo = null; 
    } 

    //return a dataset with the results 
    public function getDataSet($query, $data = NULL) 
    { 
     $start = microtime(true); 
     $cmd = $this->pdo->prepare($query); 

     $cmd->execute($data); 
     $ret = $cmd->fetchAll(); 
     //$cmd->closeCursor(); 
     $this->lastQueryTime = microtime(true) - $start; 
     $this->lastQuery = $query; 

     return $ret; 
    } 



    public function processQuery($query, $data = NULL) 
    { 
     $start = microtime(true); 
       //$this->pdo->beginTransaction(); 


     $cmd = $this->pdo->prepare($query); 
     $ret = $cmd->execute($data); 
       //$this->pdo->commit(); 
       //$cmd->closeCursor(); 
     $this->lastQueryTime = microtime(true) - $start; 
     $this->lastQuery = $query; 

     return $ret; 
    } 


    //return last insert id 
    public function lastInsertId($name = NULL) { 
     if(!$this->pdo) { 
      return false; 
     } 

     return $this->pdo->lastInsertId($name); 
    } 



    public function getOneResult($query, $data = NULL){ 
     $cmd = $this->pdo->prepare($query); 
     $cmd->execute($data); 

     return $cmd->fetchColumn(); 
    } 

    public function getError(){ 
     if($this->errorMessage != '') 
      return $this->errorMessage; 
     else 
      return true; //no errors found 

    } 

    //this where you need to set new server credentials with a new case statment 
    function setConnectionCredentials($dbName, $serv){ 

     switch($serv){ 


      //MS SQL server 
      case 'SQLSERVER': 
       $this->connString = 'sqlsrv:server='.$serv.';database='.$dbName; 
       $this->userName  = 'username'; 
       $this->passCode  = 'password'; 
      break; 

      //the defaults are predefined in the APP_configuration file - DO NOT CHANGE THE DEFAULT 
      default: 
       $this->connString = 'mysql:host='.DATABASE_HOST.';dbname='.DATABASE_NAME.';charset=utf8'; 
       $this->userName  = DATABASE_USERNAME; 
       $this->passCode  = DATABASE_PASSWORD; 
      break; 

      } 

    } 


public function lastQueryTime() { 
    if(!$this->lastQueryTime) { 
     throw new Exception('no query has been executed yet'); 
    } 
    return $this->lastQueryTime; 
} 

public function lastQuery() { 
    if(!$this->lastQuery) { 
     throw new Exception('no query has been executed yet'); 
    } 
    return $this->lastQuery; 
} 



} 



?> 

這是我用我的同班同學拉的數據集

<?php 
include('connection.php'); 

$sql_db = new connection('databaseName','SQLSERVER'); 

$call_details = $sql_db->getDataSet('SELECT 
            LocalUserId AS loginName, 
            RemoteNumberCallId AS PhoneNumber, 
            SUM(CallDurationSeconds + HoldDurationSeconds + LineDurationSeconds) AS totalTalk 
            FROM dbo.CallDetail WHERE LocalUserId = \'blah\' AND RemoteNumberCallId = \'123456789\' 
            GROUP BY LocalUserId, RemoteNumberCallId'); 


$call_details->endConnection(); 

?> 

我甚至試過這段代碼,所以我不會用我的類和我仍然得到相同的錯誤

$ss = new PDO("sqlsrv:server=SQLSERVER; Database=databaseName", "userName", "Password"); 
+0

沒有。它應該很好去..嘗試使用'sqlsrv_query'。 – Kermit 2013-04-25 20:10:03

+0

您的評論。什麼是sqlsrv_query?我該如何使用它? – Mike 2013-04-25 20:12:35

+0

** sqlsrv_query **是可替代PDO的** sqlsrv _ **系列PHP函數的一部分。這將*不會*幫助你,因爲你的問題在PHP之外,在驅動程序/操作系統級別。 – 2015-06-16 01:16:55

回答

1

我個人安裝的Windows Server 2008 R2和PHP/SQL Server的配置。

  1. 下載最新的馬廄。VC9 x86的非線程安全的PHP Downloads
  2. 提取的壓縮釋放到你的PHP目錄
  3. 下載Microsoft Drivers 3.0 for PHP for SQL Server
  4. 解壓到你的PHP目錄\ext
  5. 編寫如下擴展php.ini

    [PHP_SQLSRV_54_NTS]

    extension=php_sqlsrv_54_nts.dll

    [PHP_PDO_SQLSRV_54_NTS]

    extension=php_pdo_sqlsrv_54_nts.dll

+0

我的確如此。 但因爲我已經安裝了php5.3.19我去你在步驟3中添加此 到我的php.ini文件 [PHP_SQLSRV_54_NTS] 延長= php_sqlsrv_54_nts.dll [PHP_PDO_SQLSRV_54_NTS]延長= php_pdo_sqlsrv_54_nts.dll。 這不起作用。我不確定是什麼問題。 我是否需要在此服務器上安裝SQL?請注意,MS SQL未安裝在此服務器上(我在哪裏運行我的php腳本) 但它安裝在遠程保留位置,我試圖連接到SQL服務 – Mike 2013-04-25 21:00:49

+3

我可以VTC作爲重複的評論嗎? – Zane 2013-04-25 21:02:54

+1

以防萬一你第一次沒有得到它,@FreshPrinceOfSO – swasheck 2013-04-25 21:03:26

5

我已經更換了

extension=php_sqlsrv_53_nts.dll 
extension=php_pdo_sqlsrv_53_nts.dll

隨着

extension=php_pdo_sqlsrv_53_nts_vc9.dll 
extension=php_sqlsrv_53_nts_vc9.dll

insted的使用SQLSRV30.EXE我用SQLSRV20.EXE這個工作。

@FreshPrinceOfSo感謝您的幫助:)

謝謝:)

+1

在Win 2008 R2 x64,SQL Server標準版(64位),IIS 6和PHP 5.3以及php_pdo_sqlsrv_53_ts_vc9.dll&php_sqlsrv_53_ts_vc9.dll啓用後,我對這件事大爲惱火。 – Colin 2014-10-22 14:01:21

相關問題