为什么我可以从 PHP 连接到本地 SQL Server Express 但不能连接到远程 SQL Server?
这是使用 Windows 身份验证时的标准代码:
<?php
try {
$conn = new PDO("sqlsrv:Server=geoffrey-pc\SQLEXPRESS;Database=books",
NULL, NULL);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo $e;
die("Error connecting to SQL Server");
}
echo "Connected to SQL Server\n";
?>
上面的代码适用于连接到本地服务器(SQL Server 2008 Express Edition),但不适用于连接到网络上的服务器(SQL Server Standard Edition)。错误信息是:
异常“PDOException”,消息“SQLSTATE[28000]: [Microsoft][SQL Server Native Client 10.0][SQL Server]登录失败 用户“myDomain\GEOFFREY-PC$”。在 C:\wamp\www\PhpProject1\index.php:10 堆栈跟踪:#0 C:\wamp\www\PhpProject1\index.php(10): PDO->__construct('sqlsrv:Server=n...', NULL, NULL) #1 {main}
网络服务器的连接字符串与本地服务器的连接字符串相同,只是服务器名称类似于abc0120 并且不以 \SQLEXPRESS 或其他任何内容结尾,并且数据库名称不同。我在网络服务器上使用的数据库名称确实存在。
我正在使用 Apache 2.2.11。 SQLServer 2005 MSDN 页面:如何:使用以下方式进行连接Windows 身份验证内容如下:
Web 服务器进程(或线程)运行所依据的凭据必须映射到有效的 SQL Server 登录才能建立连接。
也许这就是问题所在。
我可以使用 SQL Server Management Studio 连接到网络服务器,也可以使用 Windows 身份验证。
This is the standard code when using windows authentication:
<?php
try {
$conn = new PDO("sqlsrv:Server=geoffrey-pc\SQLEXPRESS;Database=books",
NULL, NULL);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo $e;
die("Error connecting to SQL Server");
}
echo "Connected to SQL Server\n";
?>
The above works for connecting to the local server (SQL Server 2008 Express Edition), but not for connecting to a server on the network (SQL Server Standard Edition). The error message is:
exception 'PDOException' with message 'SQLSTATE[28000]:
[Microsoft][SQL Server Native Client 10.0][SQL Server]Login failed for
user 'myDomain\GEOFFREY-PC$'.' in C:\wamp\www\PhpProject1\index.php:10
Stack trace: #0 C:\wamp\www\PhpProject1\index.php(10):
PDO->__construct('sqlsrv:Server=n...', NULL, NULL) #1 {main}
The connection string for the network server is the same as for the local server, except that the server name is similar to abc0120 and does not end with \SQLEXPRESS or anything else, and the database name is different. The database name I use with the network server does exist.
I am using Apache 2.2.11. The SQLServer 2005 MSDN page for How to: Connect Using Windows Authentication reads:
The credentials under which the Web server's process (or thread) is running must map to a valid SQL Server login in order to establish a connection.
Maybe that is the problem.
I can connect to the network server using SQL Server Management Studio also using windows authentication.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从 PHP 手册中,您可以看到以下示例:
在您的示例中,您省略了
$user
和$password
参数。我会尝试指定用户名和密码,而不是传递null
;运行 PHP 脚本的用户可能与登录的用户不同...省略提供用户名和密码,PHP 尝试提供运行脚本的用户的凭据(甚至可能没有密码)根本没有)。From the PHP manual, you have this example :
In your example, you omit the
$user
and$password
arguments. I would try and specify a username and password instead of passingnull
; the user running your PHP script might not be the same as the logged user... omitting to provide a username and password, PHP tries to provide the user's credentials who's running the script (with perhaps even no password at all).