Connecting to MSSQL server in PHP using integrated authentication
Ok. It's hard to debug a server issue without being on the server but I've done a lot with php and SQL Server so I will do my best to share my experiences.
First, Very glad you updated from 5.3.1 that version of php is ancient and very insecure. Here are some sanity checks for your system. This may do nothing for you but all of it is worth checking.
First make sure you can connect to sql server using SQL Server Management studio with the credentials you provided. This means the same credentials you use in php not the windows authentication credentials. You should be able to have both connections at the same time so you can make changes and test the connection at the same time.
- Enable tcp. sql server configuration manager -> SQL Server network configuration -> protocols for sqlexpress -> tcp/ip (right click)-> properties -> Enabled (yes) -> ip Addresses -> IPAll -> TCP Port 1433 -> ok
- Enable sql server auth. Select server (right click) -> properties -> security -> sql server and windows authentication mode -> ok
- Open sql server port on firewall. Windows Control panel -> system and security -> windows firewall -> advanced settings -> Inbound rules -> New rule -> Port -> tcp -> 1433 (or whatever) -> Allow connection -> next -> Name -> sql server -> finish -> restart computer.
- Of course if you want to connect through a non-default user you need to add the user: sql server -> security -> logins (right click) -> add login -> server roles -> sysadmin -> ok
- If you make any of these changes restart sql server: Sql server configuration manager -> sql server services -> sql Server (right click) -> restart.
Once you confirm you can connect with management studio here are the php configuration checks:
- You can see if the extension itself is available by creating a php page with only the function
phpinfo()
in it. Then search for pdo_sqlsrv. If it is present the rest of these checks are probably not necessary but since you've been working this so long probably check them anyway. - sql_srv extension for php should be version 3.2 for php 5.6 you can obtain that library here
- Version 3.2 requires an os extension available here Check the other requirements on the previous link. Your os may use a different extension from the one linked here.
- Find your php extensions directory. this is usually {php-install-directory}/ext. Make sure you copy the appropriate version of the downloaded sqlsrv libraries into this directory. Mine are called "php_sqlsrv_55_ts.dll" and "php_dpo_sqlsrv_55_ts.dll" Yours will have 56 instead of 55 I think and the "ts" should match your php install. "ts" means thread safe, the other option is "nts" not thread safe. The one you use is dependent on your php install.
- My php.ini file contains these lines
extension=php_sqlsrv_55_ts.dll
andextension=php_pdo_sqlsrv_55_ts.dll
in that order. but I don't think order matters. and again yours will be 56 and the "ts" may be "nts". - If you made any changes based on these make sure to restart apache then check if pdo_sqlsrv is in your phpinfo() report. Also after restarting apache check the apache and php error log to see if you get specific errors about php trying to load the extensions. Post those here if you need help with them.
- You can see if the extension itself is available by creating a php page with only the function
Once you are connected to sql server through the auth creditionals in management studio and see pdo_sqlsrv in your phpinfo() here are the last things to look into in your code.
Your code above is still for mssql extension. You probably just didn't update it with your latest changes. For sql server extension your code should look like this:
$connectionInfo = array( 'UID' => $dbuser, 'PWD' => $dbpass, 'LoginTimeout' => 1, ); $host = $host . ', ' . $port; $connection = sqlsrv_connect($host, $connectionInfo); $error_messages = sqlsrv_errors();
For windows authentication exclude the uid and pwd.
$connectionInfo = array(); $conn = sqlsrv_connect( $host, $connectionInfo);
If you have more issues please tell me which step is not working so we can dig into more detail with that step.