我试图通过使用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