2014-12-05 100 views
2

我試圖通過使用sqlsrv_connect()函數連接到使用PHP的2005 Microsoft SQL數據庫。唉,函數返回false,我不知道爲什麼。通過sqlsrv_connect()連接到mssql數據庫 - PHP

<?php 
$myServer = "MAZE.jpc.wa.edu.au.local"; 
$myUser = "myUsername"; 
$myPass = "myPassword"; 
$myDB = "John Paul College"; 
$connectionInfo = array("Database"=>$myDB, "UID" => $myUser, "PWD" => $myPass); 

$conn = sqlsrv_connect($myServer, $connectionInfo); //returns false 
if($conn === false) 
{ 
    echo "failed connection"; 
} 

$sql = "SELECT name FROM users WHERE name= 'admin'"; 
$stmt = sqlsrv_query($conn,$sql); 
if(sqlsrv_fetch($stmt) ===false) 
{ 
    echo "couldn't fetch data"; 
} 
$name = sqlsrv_get_field($stmt,0); 
echo $name; 
sqlsrv_close($conn); 
?> 

有誰知道我爲什麼不能連接? 謝謝。

編輯。

好了,好了,我能彈出一個錯誤消息,感謝其他人回答,其中規定

Array (
    [0] => Array (
     [0] => IMSSP [SQLSTATE] => IMSSP 
     [1] => -49 [code] => -49 
     [2] => This extension requires either the Microsoft SQL Server 2008 Native Client (SP1 or later) 
       or the Microsoft SQL Server 2008 R2 Native Client ODBC Driver to communicate with SQL Server. 
       Neither of those ODBC Drivers are currently installed. 
       Access the following URL to download the Microsoft SQL Server 2008 R2 Native Client ODBC driver 
       for x86: http://go.microsoft.com/fwlink/?LinkId=163712 
     [message] => This extension requires either the Microsoft SQL Server 2008 Native Client (SP1 or later) 
       or the Microsoft SQL Server 2008 R2 Native Client ODBC Driver to communicate with SQL Server. 
       Neither of those ODBC Drivers are currently installed. 
       Access the following URL to download the Microsoft SQL Server 2008 R2 Native Client ODBC driver 
       for x86: http://go.microsoft.com/fwlink/?LinkId=163712) 
    [1] => Array (
     [0] => IM002 [SQLSTATE] => IM002 
     [1] => 0 [code] => 0 
     [2] => [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified 
      [message] => [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified)) 

我不完全知道如何去解決這個問題。我使用的XAMPP作爲我的測試Web服務器,PHP版本5.3及以下的.dll php_sqlsrv_53_ts_vc6.dllphp_pdo_sqlsrv_53_ts_vc6.dll

+1

您遇到的確切錯誤是什麼? – 2014-12-05 03:13:18

+0

是你的數據庫名爲'約翰保羅學院'?與所有這些空間?那麼我認爲你應該使用'$ myDB =「[John Paul College]」;' – bansi 2014-12-05 03:15:46

+0

您需要下載並安裝驅動程序,如錯誤消息所述。它幾乎解釋瞭如何解決你的問題? – Doon 2014-12-05 03:56:49

回答

4

我建議你使用sqlsrv_errors顯示連接錯誤()。請看下面的實現。

<?php 
    $serverName = "serverName\sqlexpress"; //serverName\instanceName 

    // Since UID and PWD are not specified in the $connectionInfo array, 
    // The connection will be attempted using Windows Authentication. 
    $connectionInfo = array("Database"=>"dbName"); 
    $conn = sqlsrv_connect($serverName, $connectionInfo); 

    if($conn) { 
    echo "Connection established.<br />"; 
    }else{ 
    echo "Connection could not be established.<br />"; 
    die(print_r(sqlsrv_errors(), true)); 
    } 
?> 

欲瞭解更多信息:http://php.net/manual/en/function.sqlsrv-connect.php

0

下載SQL本機客戶端的錯誤信息提示。這與SQL Server是分開的,並且需要此驅動程序正常工作。從在IIS環境中構建sqlsrv的實施中學到的經驗教訓