我試圖通過使用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.dll
和 php_pdo_sqlsrv_53_ts_vc6.dll
您遇到的確切錯誤是什麼? – 2014-12-05 03:13:18
是你的數據庫名爲'約翰保羅學院'?與所有這些空間?那麼我認爲你應該使用'$ myDB =「[John Paul College]」;' – bansi 2014-12-05 03:15:46
您需要下載並安裝驅動程序,如錯誤消息所述。它幾乎解釋瞭如何解決你的問題? – Doon 2014-12-05 03:56:49