PHP:如何建立与 SQL Server 的连接? [英] PHP: How do I establish a connection to my SQL Server?

查看:45
本文介绍了PHP:如何建立与 SQL Server 的连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常将我的 SQL Server 与 SQL Server Management Studio 一起使用.在那里登录时,我使用 Windows 身份验证并且不指定用户名/密码.因此,我目前使用的用户名/密码与登录计算机时使用的用户名/密码相同.我正在尝试通过 PHP 建立到服务器的连接;但是,我收到一个错误.有人知道我做错了什么吗?

I usually use my SQL Server with SQL Server Management Studio. When logging in there I use Windows Authentication and do not specify a Username/Password. So the username/password I'm using at the moment is the same I use logging onto my computer. I'm attempting to establish a connection to the server through PHP; however, I get the an error. Anyone know what I am doing wrong?

代码

    $serverName = "MYPCNAME"; 

    $connectionInfo = array( "Database"=>"College", "UID"=>"MYUSERNAME", "PWD"=>"MYPASSWORD");

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

错误

Connection could not be established.
Array ( [0] => Array ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user 'MYUSERNAME'. [message] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user 'MYUSERNAME'. ) [1] => Array ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user 'MYUSERNAME'. [message] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user 'MYUSERNAME'. ) )

推荐答案

我认为问题出在您的 SQL Server 配置中.您应该有一个使用 SQL Server 身份验证而不是 Windows 身份验证的用户.以下是 https 的方法://support.gearhost.com/KB/a453/adding-users-to-mssql-using-sql-server-management-studio.aspx

I think the problem is in your SQL Server configuration. You should have a user with SQL Server Authentication instead of Windows Authentication. Here is how https://support.gearhost.com/KB/a453/adding-users-to-mssql-using-sql-server-management-studio.aspx

您可能还想检查您的 SQL Server TCP/IP 配置是否可以远程访问:http://blogs.msdn.com/b/walzenbach/archive/2010/04/14/how-to-enable-remote-connections-in-sql-server-2008.aspx

And you may also want to check if your SQL Server TCP/IP configuration is accessible remotely : http://blogs.msdn.com/b/walzenbach/archive/2010/04/14/how-to-enable-remote-connections-in-sql-server-2008.aspx

这篇关于PHP:如何建立与 SQL Server 的连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆