通过 PHP 连接到 SQL Server 2008

发布于 2025-01-05 02:06:04 字数 1418 浏览 0 评论 0原文

我需要通过 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 的方法,除了一些基础知识。

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().

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

$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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雨的味道风的声音 2025-01-12 02:06:04

该错误是由注册表中的权限问题引起的。有一个密钥存储了本机客户端的路径,并且您在应用程序池中使用的身份被拒绝访问。

  • 下载 Process Monitor 并按照本文中的说明进行操作:
    http://www.iislogs.com/articles/processmonitorw3wp/(本教程介绍如何在 IIS 上执行此操作,使用 WAMP,您将需要找到在内存上运行的 .exe 进程)
  • 找到进程 w3wp.exe 所在的注册表项拒绝访问。对于 IIS,不确定 WAMP 中的进程名称是什么,但过程是相同的。
    就我而言:

    HKLM\Software\ODBC\ODBCINST.INI\SQL Native Client 10.0
    
  • 运行 regedit 并找到密钥
  • 右键单击并转到权限
  • 将网络服务添加到列表中并为其授予读取权限。
  • 回收应用程序池,它应该可以工作

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)
  • 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
关于从前 2025-01-12 02:06:04

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

sqlsrv_connect PHP manual

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));
}
涫野音 2025-01-12 02:06:04

根据您的设置,您可能需要恢复到 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 管理工具集中该用户可以访问您正在使用的数据库。那么你已经设置了账号和密码并且知道它们是什么。如果您在使用用户帐户时遇到问题,可以尝试 computerName\userName

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

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.

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文