通过PHP连接到SQL Server 2008

时间:2022-08-05 03:54:14

I need to connect to a SQL Server 2008 through PHP (WAMP, latest version). I have the sqlsrv drivers installed and set up and they do show up in phpinfo().

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

I'm using the following lines to connect to my DB, using Windows Authentication:

我使用以下行连接到我的DB,使用Windows身份验证:

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

And I'm getting the following 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
    )
)

Any help would be great, but please be specific since I really don't know my way around WAMP except for a few basics.

任何帮助都是很好的,但是请具体说明,因为除了一些基本的东西,我真的不知道如何使用WAMP。

3 个解决方案

#1


3  

The error is caused by a permission issue in the registry. There is a key where the path to the native client is stored and the identity you are using in the application pool is getting denied access.

错误是由注册表中的权限问题引起的。有一个键,用于存储到本机客户端的路径,并且正在应用程序池中使用的标识被拒绝访问。

  • 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)
  • 下载Process Monitor并遵循本文中的说明:http://www.iislogs.com/articles/processmonitorw3wp/
  • 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:

    找到进程w3wp的注册表项。exe被拒绝访问。在IIS的情况下,不确定在WAMP中进程的名称是什么,但是过程是相同的。在我的例子中:

    HKLM\Software\ODBC\ODBCINST.INI\SQL Native Client 10.0
    
  • Run regedit and find the key
  • 运行regedit并找到密钥。
  • 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
  • 回收应用程序池,它应该可以工作。

#2


2  

sqlsrv_connect PHP manual

sqlsrv_connect PHP手册

Try this approach to see what the error actually is:

尝试使用这种方法来查看错误实际上是什么:

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

#3


2  

Based on your setup, you may need to revert to the mssql_connect and corresponding functions. This works fine with mssql 2008 and you don't usually lose meaningful functionality.

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

Here's an example of setting up a connection to the database. Depending on your configuration you may have to add port information, etc.

这里有一个建立到数据库的连接的例子。根据您的配置,您可能需要添加端口信息,等等。

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

If you are running on a XAMP environment, then it would stand to reason that you are running everything locally. If that's the case, your server would be localhost or 127.0.0.1 and you can define your own accounts in SQL. As I said, I don't use Windows accounts myself, but you can add an NT account to your "server" and then in SQL Server Management tool set that user to have access to the DB you are using. Then you have set the account and password and know what they are. If you have trouble with the user account, you can try computerName\userName.

如果您在一个XAMP环境中运行,那么它将会支持您在本地运行所有东西的原因。如果是这样,您的服务器将是localhost或127.0.0.1,您可以在SQL中定义自己的帐户。正如我所说,我自己不使用Windows帐户,但是您可以在“服务器”中添加一个NT帐户,然后在SQL server管理工具中设置该用户来访问您正在使用的DB。然后,您设置了帐户和密码,并知道它们是什么。如果您的用户帐户有问题,您可以尝试计算机名称\用户名。

If you are not the admin, you'll need to get the relevant information from them.

如果你不是管理员,你需要从他们那里获得相关信息。

#1


3  

The error is caused by a permission issue in the registry. There is a key where the path to the native client is stored and the identity you are using in the application pool is getting denied access.

错误是由注册表中的权限问题引起的。有一个键,用于存储到本机客户端的路径,并且正在应用程序池中使用的标识被拒绝访问。

  • 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)
  • 下载Process Monitor并遵循本文中的说明:http://www.iislogs.com/articles/processmonitorw3wp/
  • 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:

    找到进程w3wp的注册表项。exe被拒绝访问。在IIS的情况下,不确定在WAMP中进程的名称是什么,但是过程是相同的。在我的例子中:

    HKLM\Software\ODBC\ODBCINST.INI\SQL Native Client 10.0
    
  • Run regedit and find the key
  • 运行regedit并找到密钥。
  • 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
  • 回收应用程序池,它应该可以工作。

#2


2  

sqlsrv_connect PHP manual

sqlsrv_connect PHP手册

Try this approach to see what the error actually is:

尝试使用这种方法来查看错误实际上是什么:

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

#3


2  

Based on your setup, you may need to revert to the mssql_connect and corresponding functions. This works fine with mssql 2008 and you don't usually lose meaningful functionality.

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

Here's an example of setting up a connection to the database. Depending on your configuration you may have to add port information, etc.

这里有一个建立到数据库的连接的例子。根据您的配置,您可能需要添加端口信息,等等。

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

If you are running on a XAMP environment, then it would stand to reason that you are running everything locally. If that's the case, your server would be localhost or 127.0.0.1 and you can define your own accounts in SQL. As I said, I don't use Windows accounts myself, but you can add an NT account to your "server" and then in SQL Server Management tool set that user to have access to the DB you are using. Then you have set the account and password and know what they are. If you have trouble with the user account, you can try computerName\userName.

如果您在一个XAMP环境中运行,那么它将会支持您在本地运行所有东西的原因。如果是这样,您的服务器将是localhost或127.0.0.1,您可以在SQL中定义自己的帐户。正如我所说,我自己不使用Windows帐户,但是您可以在“服务器”中添加一个NT帐户,然后在SQL server管理工具中设置该用户来访问您正在使用的DB。然后,您设置了帐户和密码,并知道它们是什么。如果您的用户帐户有问题,您可以尝试计算机名称\用户名。

If you are not the admin, you'll need to get the relevant information from them.

如果你不是管理员,你需要从他们那里获得相关信息。