简体   繁体   English

PDO连接到MS SQL Server 2008

[英]PDO connection to MS SQL Server 2008

I have done this once before in a VM, however, I needed to create a second instance of a VM, and I am trying to connect to my MS SQL Server. 我曾经在VM中完成过一次此操作,但是,我需要创建VM的第二个实例,并且试图连接到MS SQL Server。 Currently I have php-5.3.6-nts-Win32-VC9-x86 version of php installed. 目前,我已经安装了php-5.3.6-nts-Win32-VC9-x86版本的php。 I have correctly configured my php.ini and running phpinfo() I can verify that I have sqlsqrv PDO drivers installed correctly. 我已经正确配置了php.ini并运行了phpinfo() ,可以验证是否已正确安装了sqlsqrv PDO驱动程序。 I am using this in conjunction with IIS 7, and I have the webserver up and running. 我将其与IIS 7结合使用,并且已启动并运行了Web服务器。 I have the following line in my index.php file 我的index.php文件中包含以下行

$conn = new PDO("sqlsrv:server=$serverName; Database=classReg");

I also have the following to give me some feedback on if I made a successful connection to the database. 如果成功建立数据库连接,我还可以提供以下反馈。

if($conn) {
    echo "Connection established. </br></br>";
} else {
    echo "Connection could not be established. </br>";
    die(print_r(sqlsrv_errors(), true));
}

Problem is, I'm not getting anything out of my $conn variable. 问题是,我没有从$ conn变量中得到任何东西。 I've tried to echo that variable, and I'm not getting any feedback. 我试图回显该变量,但没有得到任何反馈。

FWIW my php.ini file reads these three lines at the end of the file FWIW我的php.ini文件在文件末尾读取了这三行

[PHP_MS_SQL_PDO]
extension=php_sqlsrv_53_nts_vc9.dll
extension=php_pdo_sqlsrv_53_nts_vc9.dll

Any ideas as to what I may be missing? 关于我可能缺少的任何想法吗?

I was able to fix the issue by doing the following: 通过执行以下操作,我得以解决此问题:

  • I created a user to access the database with in place of Windows Authentication. 我创建了一个用户来代替Windows身份验证来访问数据库。
  • I also added IUSR to the database logins group. 我也将IUSR添加到数据库登录组。

After doing those two things, I modified the following line of code to connect to the database. 完成这两件事后,我修改了以下代码行以连接到数据库。

$conn = new PDO("sqlsrv:server=$serverName; Database=classReg");

This line now reads: 该行现在显示为:

$conn = new PDO("sqlsrv:server=$serverName; Database=classReg", "<user>", "<password>");

One thing that helped me out was looking at the logs for the server. 帮助我的一件事是查看服务器的日志。 I was having an issue, because I wasn't getting any output from my if statement in the original code. 我遇到了一个问题,因为我没有从原始代码中的if语句获得任何输出。 But I was able to look at the logs from SQL Server, and I was able to see what was happening. 但是我能够查看来自SQL Server的日志,并且能够看到正在发生的事情。 The logs are located at Management -> SQL Server Logs . 日志位于Management -> SQL Server Logs After I made those two changes, I was able to successfully connect to the database without any troubles. 完成这两项更改后,我就可以成功连接到数据库而没有任何麻烦。 I hope this helps anyone else that is running into an issue like this! 希望这对遇到此类问题的其他人有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM