Using PDO's odbc to connect to a MSSQL server instance with Windows Authentication

瘦欲@ 提交于 2019-12-13 02:06:30

问题


I'm trying to connect to a MSSQL database using PDO with odbc. I'm aware that there's a package SqlSrv (but for some reason that package (.dll) won't load properly). So I found some documentation arguing it's also possible with PDO. In my PHP.ini I've enabled the extension php_pdo_odbc.dll and it loads fine.

My connection string looks like this:

$conn = new PDO(
    'odbc:
        Driver=SQL Server;
        Server=MyServer\MyInstance;
        Database=MyDatabaseName;
        Trusted Connection=Yes;',
    'MyWindowsUserName',
    'MyWindowsPassword'
);

I've tried various properties (for example by prepending the domain to the username, switching with the authentication options User Id, UID, Password, PWD and Trusted Connection) but I keep getting the message

SQLSTATE[28000] SQLDriverConnect: 18456 [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'MyWindowsUserName'.

Any suggestions on how to connect to the database with my Windows Account? (that's the only way for me to connect to the database)


回答1:


Try removing the username & password

$conn = new PDO(
    'odbc:
        Driver=SQL Server;
        Server=MyServer\MyInstance;
        Database=MyDatabaseName;
        Trusted Connection=Yes;'
);



回答2:


I have authenticated by Windows with following PHP statement: This is my code:

$ Conn = new PDO ("odbc: Driver = {SQL Server}; Server=JAMILI-PC\SQLEXPRESS; null; null");
  • I am using Windows 2008.
  • I hope it solves your problem.


来源:https://stackoverflow.com/questions/29606929/using-pdos-odbc-to-connect-to-a-mssql-server-instance-with-windows-authenticati

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!