通过PHP连接到SQL Server 2008

6

我需要通过PHP(WAMP,最新版本)连接到SQL Server 2008。我已经安装并设置了sqlsrv驱动程序,并且它们确实显示在phpinfo()中。

我正在使用以下行连接到我的数据库,使用Windows身份验证:

$serverName = "(local)";
$connectionOptions = array("Database"=>"MyTestDatabase");
$conn = sqlsrv_connect( $serverName, $connectionOptions) or die("Error!");

我遇到了下面的错误:

Array 
( 
    [0] => Array 
    ( 
        [0] => IMSSP 
        [SQLSTATE] => IMSSP 
        [1] => -49
        [code] => -49 
        [2] => This extension requires the Microsoft SQL Server 2011 Native Client. Access the following URL to download the Microsoft SQL Server 2011 Native Client ODBC driver for x86: http://go.microsoft.com/fwlink/?LinkId=163712 
        [message] => This extension requires the Microsoft SQL Server 2011 Native Client. Access the following URL to download the Microsoft SQL Server 2011 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
    )
)

任何帮助都会很棒,但请具体说明,因为除了一些基础知识外,我真的不熟悉WAMP。
3个回答

3

这个错误是由于注册表中的权限问题引起的。存在一个键,其中存储了本地客户端的路径,您在应用程序池中使用的身份被拒绝访问。

  • Download Process Monitor and follow the instructions on this post: http://www.iislogs.com/articles/processmonitorw3wp/ (This tutorial shows how to do it on IIS, with WAMP you will need to find the .exe process that runs on memory)
  • Find the registry key where the process w3wp.exe is being denied access. That in the case of IIS, not sure what's the name of the process in WAMP but the procedure is the same. In my case:

    HKLM\Software\ODBC\ODBCINST.INI\SQL Native Client 10.0
    
  • Run regedit and find the key
  • Right click and go to Permissions
  • Add Network Service to the list and give it Read permissions.
  • Recycle the app pool and it should work

是的...你说得对。这对我也起作用。我一段时间前就偶然发现它了,甚至忘记了我在这里提问过。感谢您抽出时间确认这个:D - Eugen
但是如果在任务管理器中找不到"w3wp.exe"怎么办..?? - Name is Nilay
@NilayOnAndroid w3wp.exe 是 IIS 进程。如果你没有它在运行,那就意味着你的 IIS 没有在运行。 - Federico Giust
@FedericoGiust-那是不是意味着我不能连接到我的SQL Server 2008..?抱歉问这么愚蠢的问题,因为我是新手在SQL连接方面。 - Name is Nilay
@NilayOnAndroid 你是在尝试连接到mysql还是ms sql?你正在运行哪个服务器?IIS,Apache(WAMP,XAMPP)? - Federico Giust
@FedericoGiust 对不起,那是我的打字错误...我想连接到 MSSQL 数据库,我正在使用 Apache 2.2.22 和 PHP 5.3.1 的 WampServer。 - Name is Nilay

2

sqlsrv_connect PHP 手册

尝试以下方法,查看实际错误信息:

$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));
}

我已经编辑了我的帖子,并附上了我现在遇到的错误。感谢您抽出时间来回答。 - Eugen
从您的错误信息来看,似乎您没有安装Microsoft SQL Server 2011本机客户端。它提供了一个下载链接。请参见此处的第三篇帖子:http://forum.wampserver.com/read.php?2,53541,printview,page=1 - Jeremy

2

根据您的设置,您可能需要恢复使用mssql_connect和相应的函数。这在mssql 2008上运行良好,通常不会失去有意义的功能。

以下是设置与数据库建立连接的示例。根据您的配置,您可能需要添加端口信息等。

$hostname = "server.domain.com";
$database = "DatabaseName";
$username = "LocalSQLUserName";
$password = "P@ssw0rd";
$Connection = mssql_connect($hostname, $username, $password);

如果您在XAMP环境下运行,那么您很可能正在本地运行所有内容。如果是这种情况,您的服务器将是 localhost127.0.0.1,并且您可以在 SQL 中定义自己的帐户。如我所说,我自己不使用 Windows 帐户,但您可以将 NT 帐户添加到您的 "服务器",然后在 SQL Server Management 工具中将该用户设置为访问您正在使用的数据库。然后您就已经设置好了帐户和密码,并知道它们是什么。如果您有用户帐户的问题,可以尝试使用 computerName\userName
如果您不是管理员,则需要从管理员处获取相关信息。

我不需要太多的功能,只需要将一些基本查询转换成JSON格式。还有一个问题...如果我使用最简单、默认的Windows身份验证,那么"server-name"、"user"和"password"参数是什么? - Eugen
我很快就会到办公室,然后填写我的答案。 - AlexC
@Eugen,我已经编辑了我的答案,展示了我的连接示例。至于Windows身份验证,出于安全原因,我个人避免使用它。我总是为特定数据库创建一个本地帐户。 - AlexC
这里有一些可能会有所帮助的链接。它们有点过时,但仍然有效:http://www.phpfreaks.com/forums/index.php?topic=197064.msg888989#msg888989 和 http://stackoverflow.com/questions/6674320/fatal-error-call-to-undefined-function-mssql-connect-in-php - AlexC
感谢您抽出时间来帮助我!我最终通过使用XAMPP 1.7.3使mssql_connect工作了,但现在我需要弄清楚Windows身份验证的服务器名称、用户名和密码是什么。 - Eugen
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接