16

I recently installed IIS, PHP 5.3.5 and MySQL, and had it all working.

I then has a requirement for MS SQL as well, and what I thought would be an easy task, is now driving me crazy and I can't figure out how to make it work.

According to php_info() the MS SQL drivers aren't loading.

I downloaded php_mssql.dll and enabled it in php.ini but it didn't work. I found out that the new version of PHP uses a different dll. So I downloaded the new one, put that in the ext folder, and updated the php.ini, but still no luck.

php_info() still isn't showing the drivers are loaded.

I am about to uninstall IIS and PHP, and install Apache, which I've installed before.

This question is my last attempt, can anyone help?

Kev
  • 118,037
  • 53
  • 300
  • 385
Naner
  • 1,292
  • 6
  • 25
  • 43
  • Just grabbing the DLL files isn't enough -- are you also modifying `php.ini` to load them? – Charles Mar 24 '11 at 20:58
  • Yes, I modified the php.ini file and restarted IIS. I Tried both php_mssql.php and php_sqlsrv_53_nts_vc9.dll. – Naner Mar 24 '11 at 21:08
  • Are you modifying the correct `php.ini` file? It should list the correct filename in a `phpinfo` page. – Charles Mar 24 '11 at 21:22
  • I'm assuming that you use FastCGI, right? Does IIS shut down the external FastCGI bits during a restart? I'm grasping at straws here... – Charles Mar 24 '11 at 21:29
  • Ops... I am using FastCGI, I tried restarting IIS and also stopping and starting again, as well as restarting the computer. – Naner Mar 24 '11 at 21:33
  • I hate to ask obvious questions, but is the extensions directory as defined in php.ini the directory in which you placed the DLLs, and is it the same directory as is listed in `phpinfo`? I'm out of ideas... – Charles Mar 24 '11 at 21:37
  • It is man, checked that also. Otherwise MySQL wouldn't be working. – Naner Mar 24 '11 at 21:47
  • Good point. Is PHP complaining in the error log about the extension not loading? – Charles Mar 24 '11 at 21:50
  • I haven't checked that one, but I'm not in that computer right now, but I'll do that in about an hour. I did some research and found something about sql native client and the sql driver. Does this ring a bell at all? – Naner Mar 24 '11 at 21:56
  • All I know is that Microsoft recently rebuilt their own MSSQL driver for PHP, which you seem to have come across -- it's the "sqlsrv" one. – Charles Mar 24 '11 at 22:01
  • Actually, they gave serveral different files, and I used the nts one, dv9. I am running windows 7 64bits. I checked the version that PHP was compiled, which was dv9, and they said that if I had php5.dll I should use nts. And... still. I've got to be missing something. – Naner Mar 24 '11 at 22:05
  • The sqlsrv driver does have a dependency on the SQL Server Native Client. Download links and more info are on this page: http://msdn.microsoft.com/en-us/library/cc296170.aspx. – Brian Swan Mar 24 '11 at 22:36
  • In addition to Brian's link, you also might need a particular version of the driver. The ZTS-ness of the driver should match the ZTS-ness of PHP, if possible. (ZTS => Zend Thread Safe) – Charles Mar 24 '11 at 22:46
  • Hey guys, I tried to load the two dlls: php_sqlsrv_53_nts_vc9.dll, and php_pdo_sqlsrv_53_nts_vc9.dll. But apparentely it didn't work, I opened the php.ini file, and it doesn't show MSSQL. How do I check the errors that I'm getting? – Naner Mar 25 '11 at 00:51
  • Ops, I opened the php_info() and it didn't show MSSQL. Sorry – Naner Mar 25 '11 at 00:58

2 Answers2

37

This is a snippet from a customer FAQ I wrote up a wee while ago to help our dedicated hosting customers get PHP up and running with the MS SQL Drivers. It may duplicate some knowledge already imparted in the comments above, but it's completeness may also help others starting from scratch:

Note: Clearly the PHP version numbers have moved on a bit since I wrote this but the general principles still apply.


Installation Pre-requisite: The SQL Native Client is required for the Microsoft Drivers for PHP for SQL Server:

Microsoft SQL Server 2008 R2 Native Client X64 - for 64 bit Windows
or
Microsoft SQL Server 2008 R2 Native Client X32 - for 32 bit Windows

Download and install the Native Client driver that matches your system.

PHP MS SQL Drivers:

Download the Microsoft Drivers for PHP for SQL Server:

Microsoft Drivers for PHP for SQL Server

The file is a self-extracting executable so just use 7zip or WinRAR and extract the files to a folder of your choice. We now need to decide which driver to choose that matches your PHP installation.

PHP 5.3.5 comes in four different flavours:

  • PHP 5.3.5 Non-threadsafe VC9
  • PHP 5.3.5 Non-threadsafe VC6
  • PHP 5.3.5 Threadsafe VC9
  • PHP 5.3.5 Threadsafe VC6

To tell which version you have installed open the snapshot.txt file which lives in the same folder as php.exe, php-cgi.exe etc. Don't use notepad.exe because the file uses just line feed (\n) for line ends (unix format) at the start and notepad mangles these into a single line.

In the file you will see a line (around line 6, or near the middle of line 1 of you did use notepad) starting with: Build::

Build: D:\php-sdk\snap_5_3\vc9\x86\obj\Release - non-threadsafe, VC9 build
Build: D:\php-sdk\snap_5_3\vc6\x86\obj\Release - non-threadsafe, VC6 build
Build: D:\php-sdk\snap_5_3\vc9\x86\obj\Release_TS - threadsafe, VC9 build
Build: D:\php-sdk\snap_5_3\vc6\x86\obj\Release_TS - threadsafe, VC6 build

This tells us what version of PHP you're running.

From the folder you extracted the MS SQL PHP drivers, choose the driver(s) that matches the version of PHP being used (if you're not using PDO then you don't need to copy the php_pdo_ drivers):

PHP 5.3.5 Non-threadsafe VC9: php_sqlsrv_53_nts_vc9.dll, php_pdo_sqlsrv_53_nts_vc9.dll
PHP 5.3.5 Non-threadsafe VC6: php_sqlsrv_53_nts_vc6.dll, php_pdo_sqlsrv_53_nts_vc6.dll
PHP 5.3.5 Threadsafe VC9: php_sqlsrv_53_ts_vc9.dll, php_pdo_sqlsrv_53_ts_vc9.dll
PHP 5.3.5 Threadsafe VC6: php_sqlsrv_53_ts_vc6.dll, php_pdo_sqlsrv_53_ts_vc6.dll

Assuming your PHP installation lives in C:\PHP, copy (DON'T MOVE) these files to your C:\PHP\EXT folder.

Open C:\PHP\PHP.INI and locate the Dynamic Extensions part of the file and add:

extension=php_sqlsrv_53_nts_vc9.dll
extension=php_pdo_sqlsrv_53_nts_vc9.dll   <-- optional

Finally, to be sure that PHP can find these extensions, ensure the extension_dir directive in C:\PHP\PHP.INI is set to C:\PHP\EXT:

extension_dir = C:\PHP\ext

Restart IIS and call phpinfo(). If all is well you should see:

enter image description here

And if you also loaded the PDO driver you should see:

enter image description here

Kev
  • 118,037
  • 53
  • 300
  • 385
  • 1
    Awesome, thank you. Does that mean that the mssql_ functions are no longer available?? – Naner Mar 25 '11 at 23:08
  • 3
    @Naner - thanks :). Yes, you need to use the `sqlsrv_` functions instead, which nearly map onto the `mssql_` functions but there are some differences: http://msdn.microsoft.com/en-us/library/cc296152.aspx – Kev Mar 26 '11 at 00:03
  • How am I supposed to set up this sqlsrv driver on linux ? Please find my answer on the link below and please give me necessary suggestions.http://stackoverflow.com/questions/11839368/installing-sqlsrv-driver-on-linux – hsuk Aug 07 '12 at 04:56
  • @KusH - The driver's described above are Windows only. I have to be honest and say that I've never had a use case for accessing MS SQL from PHP on Unix. You could have a look at: http://stackoverflow.com/questions/3684782/php-ms-sql-unix-driver-microsoft-or-freetds to see if that helps any. – Kev Aug 07 '12 at 13:56
  • @hotohoto not tried but I would guess so. The problem tends to be with PHP drivers lagging behind PHP releases. – Kev Jun 29 '15 at 09:04
1

The above mentioned:

extension=php_sqlsrv_53_nts_vc9.dll
extension=php_pdo_sqlsrv_53_nts_vc9.dll

what I found is incorrect.

Therefore, it is likely to be:

extension=php_sqlsrv_53_ts_vc9.dll
extension=php_pdo_sqlsrv_53_ts_vc9.dll
Raunak
  • 17
  • 1
  • 7