将php应用程序连接到MS SQL服务器

将php应用程序连接到MS SQL服务器

问题描述:

TLDR: I'd like to connect a php application to a MS SQL server through an active directory account but haven't had much luck finding documentation on how to do that.

Most of the issues that people seem to have with this are pretty complicated I think I just don't have a strong enough grasp of the concept. When I try to connect it to the server, I get: Warning: mysql_connect(): No connection could be made because the target machine actively refused it.

The code I'm using is:

$user_name = "ADAccountName";
$pass_word = "ADAccountPW";
$datbase = "myDatabase";
$server = "#ipaddress#:#port#";

$db_handle = mysql_connect($server, $user_name, $pass_word);
$db_found = mysql_select_db($database, $db_handle);

I created a user "sa" through MS SQL Server Management Studio and attached it to an AD account (I don't seem to have access to create one without picking an AD account).

I've had luck connecting it to a database using MySQL Workbench (for testing) and a dedicated username/password, but the actual database I need to use is run by MS SQL Server Manager with an AD account. I've spent the greater part of the day going through forums and Microsoft's documentation on SQL Server Manager but I haven't found anything that can help me out with this.

I'm using php v. 5.2

TLDR:我想通过一个活动目录帐户将一个php应用程序连接到一个MS SQL服务器但是没有' 很幸运找到了如何做到这一点的文档。 p>

人们似乎对此大多数问题都非常复杂我认为我对此没有足够的把握 这个概念。 当我尝试将它连接到服务器时,我得到: 警告:mysql_connect():无法建立连接,因为目标计算机主动拒绝它。 p>

我正在使用的代码 是: p>

  $ user_name =“ADAccountName”; 
 $ pass_word =“ADAccountPW”; 
 $ datbase =“myDatabase”; 
 $ server =“#ipaddress#  :#port#“; 
 
 $ db_handle = mysql_connect($ server,$ user_name,$ pass_word); 
 $ db_found = mysql_select_db($ database,$ db_handle); 
  code>  pre> \  n 
 

我通过MS SQL Server Management Studio创建了一个用户“sa”并将其附加到一个AD帐户(我似乎无权创建一个而不选择AD帐户)。 p> \ n

我很幸运使用MySQL Workbench(用于测试)和专用的用户名/密码将其连接到数据库,但我需要使用的实际数据库由带有AD帐户的MS SQL Server Manager运行。 我花了大部分时间通过论坛和Microsoft的SQL Server Manager文档,但我没有发现任何可以帮助我解决这个问题。 p>

我正在使用 php v.5.2 p> div>

If you're trying to connect to MSSQL you will need to do a few things to your web server first.

1.You will need to add PHP to your windows startup More Info

2.Then you will need to install SQL Server Native client

3.Install the SQL Server drivers for your web server. SQLSRV30 is for PHP 5.4, SQLSRV31 is for PHP 5.5

4.Then go to your php.ini file and enable these extensions.

This will enable you to use PHP Data Objects.

You can connect to the DB like this:

try
{
    $conn = new PDO( "sqlsrv:server=$serverName ; Database=AdventureWorks", "sa", "");
    $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
catch(Exception $e)
{ 
    die( print_r( $e->getMessage() ) ); 
}

Depending which version of PHP you are using you could do the following

resource mssql_connect ([ string $servername [, string $username [, string $password [, bool $new_link = false ]]]] )

URL - http://php.net/manual/en/function.mssql-connect.php

This would give you direct connection to the DB.