Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting to SQL Server using PHP, ODBC, and Windows authentication

I'm trying to use PHP to connect to an ODBC data source using Windows authentication. I can connect just fine to the server in SQL Server so I know it's running. When I try to run the command

$link = odbc_connect("my_odbc","",""); 

I get the error:

"Warning: odbc_connect(): SQL error:
[Microsoft][ODBC Driver 11 for SQL Server][SQL Server]
Login failed for user ''., SQL state 28000 in SQLConnect in C:\Users..."

I tried:

$link = odbc_connect("Driver={ODBC Driver 11 for SQL Server};   
        Server='my_odbc';Integrated Security=SSPI","","");

Which returned the message:

Warning: odbc_connect(): SQL error:
[Microsoft][ODBC Driver 11 for SQL Server]
Named Pipes Provider: Could not open a connection to SQL Server [53]. , SQL state 08001 in SQLConnect in C:\Users..."

Not sure what I'm doing wrong.

  • my_odbc is a SQL Server (2008) on a different machine. I don't have admin privileges on that database so I can't change anything on that end (such as enabling SQL Server authentication).

  • I am running Windows 7 and using PHP Version 5.6.12

  • phpinfo() indicates that ODBC Support is enabled as well as pdo_sqlsrv support

like image 269
Compysaurus Avatar asked Aug 17 '15 21:08

Compysaurus


People also ask

How do I connect to SQL Server with Windows authentication?

Open SQL Server Management Studio. In Connect to Server, select Database Engine, enter your SQL Server name, and enter administrator credentials to connect to the server. Select Connect. In Object Explorer, expand the SQL Server, expand Security, right-click Logins, and then select New Login.

Can a SQL Server Login use Windows authentication?

A connection made using Windows Authentication is sometimes called a trusted connection, because SQL Server trusts the credentials provided by Windows. By using Windows Authentication, Windows groups can be created at the domain level, and a login can be created on SQL Server for the entire group.

How do I connect to SQL Server with SQL authentication?

On the Security page, under Server authentication, select the new server authentication mode, and then click OK. In the SQL Server Management Studio dialog box, click OK to acknowledge the requirement to restart SQL Server. In Object Explorer, right-click your server, and then click Restart.

How do I enable both Windows and SQL Server authentication?

In the Object Explorer, right-click the server and click Properties. On the Security page under Server authentication, select SQL Server and Windows Authentication mode and then click OK.


2 Answers

Maybe you should try PDO (the performance difference isn't that great) with SQLsrv plugin (this what I'm using to connect to my other boss' software which use SQL Server 2008 database):

$connection = new PDO("sqlsrv:Server=" . $this->sourceServer . ";Database=" . $this->sourceDB, $this->sourceUser, $this->sourcePW);
$connection->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

You can download the plugin here

https://www.microsoft.com/en-ca/download/details.aspx?id=36434

In Xampp, you must copy the dll in that folder:

C:\xampp\php\ext

And you must add that line to your php.ini

extension = php_pdo_sqlsrv_56_ts.dll

Note: Don't forget to restart your server so it can take in account the new php.ini file.

Let me know if it works for you

like image 105
Jonathan Parent Lévesque Avatar answered Oct 27 '22 23:10

Jonathan Parent Lévesque


Someone had the same error message and asked and answered his own question here.

To quote...

I´m sorry for the troubles.

The problem was, that I was using SQL Server Native Client 11.0 as driver. I switched it to SQL Server and now it works :/

Hopefully this at least helps someone, being in a similar problem....

like image 29
ourmandave Avatar answered Oct 27 '22 22:10

ourmandave