Installing PHP 5.3 on Microsoft IIS

July 4th, 2009 | Tags: , ,

Update: This article is considered outdated. I’ve started an IIS Tutorial Series on how to install PHP and FastCGI on IIS 6.0.

With the release of PHP 5.3.0 introducing a lot of new features and bug fixes, it also comes in with a lot of changes for Microsoft IIS users.  The biggest one of them is the way that PHP is now installed on IIS.  The PHP 5.3 package now no longer provides an ISAPI DLL file (which I’ve been using).  The only way to do it is through CGI, in which you should (have to?) use the FastCGI module.  Depending on which version of IIS you have, the setup will be different.  (I have IIS 6.0 on a Windows 2003 server).  Microsoft seems to have gotten more close with Zend recently and provided better support on IIS that they even have a dedicated website for installing PHP on IIS and providing community support.  Everything you need to know for installing PHP on IIS is on that website.  PHP also now have a website dedicated to supporting PHP on Windows, regardless of whether you’re using IIS or Apache on Windows.

One of the big drawbacks I’ve found was that PHP decided to remove the MS SQL Server connection driver in the PHP 5.3 package.  Just adding back in the DLLs from the old PHP packages back in the PHP directory did not work for me.  Not only did they make it harder for IIS users to install PHP, they had to make it harder for those to connect to MS SQL Server.  Upon doing some online searches, the driver used in PHP was very old (which explains my post on trying to get it to work properly on previous versions of PHP).  Microsoft now has it’s own version of MS SQL Server driver (2005 and above) for PHP.  I have yet to install it, but it would have to be done on a development environment to redo any code using PHP’s old connection library.  I’ve also yet to find any documentation on it so I’m not sure of the details of the driver.

Update: September 24, 2009
Maybeme compiled a php_mssql.dll that works in the VC9 version of PHP 5.3. Note that the ntwdblib.dll files is still necessary. I haven’t gotten it to work personally, but it seems others have, so I’m sharing it for others who wants to try.

Download here: php_mssql.dll (right-click > Save Link As…)

  1. Maybeme
    August 11th, 2009 at 02:46
    Reply | Quote | #1

    Hi,

    I compiled the php_mssql.dll myself for PHP 5.3.0.
    You can download it here:
    http://rapidshare.com/files/262914922/php_mssql.dll

    You still need the ntwdblib.dll (see php.net for details)

  2. Maybeme
    August 11th, 2009 at 02:49
    Reply | Quote | #2

    Ow, I forgot to mention:
    The dll is compiled with MS VC9, so it only works with the VC9 version of PHP, which is only usable with IIS or Apache from apachelounge.org
    (So NOT the usual apache from apache.org)

  3. August 11th, 2009 at 19:57
    Reply | Quote | #3

    Maybeme,

    Thanks for sharing! I’ll try it out in the next couple of days when I get the chance and update my post for anyone else who might be interested in the file also. :)

  4. Brian
    September 24th, 2009 at 05:36
    Reply | Quote | #4

    @Maybeme:

    That DLL worked GREAT (IIS 6, Server 2k3, PHP 5.3.0). Now I don’t have to recode our apps to work with the MSFT provider driver.

    Thank you so much

  5. September 24th, 2009 at 22:19
    Reply | Quote | #5

    Brian,

    You got it to work fine? That’s great. Did you have to do anything special by any chance aside from adding the extension to php.ini? I took a shot at it but was not able to get it to work. I have the same server specs also. It was a while ago so I don’t remember exactly what the start-up error was, but didn’t have the chance to try it again. I’ll post an update to this post with a link to the file.

  6. Jojo
    December 14th, 2009 at 07:59
    Reply | Quote | #6

    ntwdblib.dll files, what is that?? and how we can allow and apply the mssql connection?!

  7. December 15th, 2009 at 00:33
    Reply | Quote | #7

    Jojo,

    Sorry for my small typo. It’s “file”, not “files”. ntwdblib.dll is a library file in the PHP directory that is needed to talk to the MS SQL Server.

    You can try to search online to find the file. I, however, have not gotten it to work. I read somewhere that the old php_mssql.dll library is supposedly really outdated and lacked maintenance.
    I’ve tried the one I’ve uploaded on this post at:
    http://jimmyli.net/2009/03/php-unable-to-connect-to-mssql/
    It’s for PHP 5.2.x, so I don’t know if it’ll work if you’re using PHP 5.3.x.

    I recommend using Microsoft’s official SQL Server Driver for PHP now that they have one, regardless of whether you’re on PHP 5.2 or 5.3.
    Here’s a post I made about it for PHP 5.3:
    http://jimmyli.net/2009/09/finally-a-working-sql-server-driver-for-php-5-3/
    I was able to get it to work successfully on PHP 5.3.

  8. Alex
    January 4th, 2010 at 11:04
    Reply | Quote | #8

    Hi Jimmy:
    The dll works fine with Thread Safe PHP 5.3. Could you give us a version compiled against Non-Thread Safe PHP 5.3?
    Thanks

  9. July 29th, 2010 at 06:43
    Reply | Quote | #9

    Has anyone got it to work on SQL Server 2000? All the fixes and updates only talk about 2005 and up. Soryy, but my budget don’t permit an upgrade. Plus too much legacy stuff that I can’t yet move over to MySQL or I would have that route….

    Anyway, Windows Server 2K3, IIS 6, PHP 5.3 and MS SQL 2000 – any advice?

    Cheers!

  10. July 29th, 2010 at 18:28

    Anil,

    On the test server I used I have the same server and software specs you listed, which is with SQL Server 2000. I haven’t touched this setup in a while, but I think I was able to get it to work with the latest SQL Server Driver for PHP, even if it’s stated for SQL Server 2005. Have you tried it out yet?

    I’ll check my server again when I have the chance and I’ll post here again.

  11. July 30th, 2010 at 11:44

    I will try it. I assume you mean the SQL Server Driver for PHP found on Microsoft.com?

    But also, I can’t find a 5.3 distro compiled with MSSQL. The closest appears to be a 5.2 non-thread safe distro which apparently cwas compiled with MSSQL support. I have never compiled PHP before, so I am dreading doing that as well.

    I almost broke down and installed SQL Server 2008 Express (It did not install – lucky for me :-)). Still wondering if it can coexist with SQL Server 2000? Then the other option is to get MySQL for Windows.

  12. July 30th, 2010 at 12:26

    Anil,

    I too think that compiling your own PHP with the old MSSQL extension is not a good idea, especially to make unofficial features work, and the extension is considered buggy.

    Anyway, I looked into my test install today and here’s what I found. Note that I did mess around a lot with installing and uninstalling things before and I don’t remember exactly what I’ve done. The server’s a mess. But I believe here’s what I did that did get PHP5.3 talking to MSSQL 2000. And this is an official method supported by Microsoft.

    First, make sure your PHP5.3 and IIS installation is working as expected.

    Visit this page:
    http://www.microsoft.com/downloads/details.aspx?familyid=B33D2C78-1059-4CE2-B80D-2343C099BCB4
    Scroll down (or search) on the page for this section: Microsoft SQL Server 2008 Native Client
    You’ll note the description of it stating that it can connect to SQL Server 2000, 2005 and 2008. Download MSI package appropriate for your server, and install it.

    Visit this page:
    http://www.microsoft.com/downloads/details.aspx?FamilyID=ccdf728b-1ea0-48a8-a84a-5052214caad9
    - Download the file (button above the Quick Details box): SQLServerDriverForPHP11.EXE
    - Executing it will extract a folder of files. Put in anywhere you want (e.g. Desktop). In it will be a set of DLLs. Choose the one appropriate for the PHP package you are using (look at the readme if you need more info.)
    - Copy the appropriate DLL and paste it in your PHP’s ext folder.
    - Open up your php.ini and add the line to the extensions section enable the DLL extension you copied for PHP, and save.
    e.g. extension=php_sqlsrv_53_nts_vc9.dll

    Restart your web server for all settings to take affect.
    Run a phpinfo(); script and check to make sure “sqlsrv” is there somewhere.
    In that SQL Server Driver folder you extracted earlier, double-click and run the help file, SQLServerDriverForPHP.chm.
    This will pretty much have everything you need to know in getting your PHP to talk to SQL Server including the API functions to use in your script.

    Hope that works!
    If that works for you, maybe I’ll add a new post here detailing that so it can help others. Thanks!

  13. August 5th, 2010 at 04:41

    I think it worked, but I am not sure. The database I need uses Windows Authentication and I get the following error:
    Array ( [0] => Array ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [ code] => 18456 [2] => [Array ( [0] => Array ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [ code] => 18456 [2] => [Microsoft][SQL Server Native Client 10.0][SQL Server]Login failed for user ”. [message] => [Microsoft][SQL Server Native Client 10.0][SQL Server]Login failed for user ”. ) [1] => Array ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [ code] => 18456 [2] => [Microsoft][SQL Server Native Client 10.0][SQL Server]Login failed for user ”. [message] => [Microsoft][SQL Server Native Client 10.0][SQL Server]Login failed for user ”. ) )

    I have even tried the Administrator login as the user name. If I don’t specify the user/password combination, it of course uses the IUSR_, but that does not work either (naturally, I would expect that).

    Any advice – I feel I am close! :-)

  14. August 5th, 2010 at 09:52

    Anil,
    Seems like you got the SQL Server library working in PHP, at least! So you’re definitely close! It sounds like it can talk to the DB server too, just not logging in.
    According to the docs it should log in using Windows Authentication credentials. I use SQL Server Authentication, though.

    If you could post the code on how you’re connecting I can help take a look at it.

    Did you also try including the DOMAIN name your Windows User is on?

    Your code should probably look something like this:

    $connectionInfo = array('UID'=>'DOMAIN/Username', 'PWD'=>'password', 'Database'=>'Northwind');
    $conn = sqlsrv_connect('MY-DBSERVER', $connectionInfo);
    if ($conn === false) die( print_r( sqlsrv_errors() ) );
    
  15. August 5th, 2010 at 09:59

    Anil,
    Sorry, I’m not sure what I posted might work or not since I now found this page:
    How to: Connect Using Windows Authentication
    http://msdn.microsoft.com/en-US/library/cc296205%28v=SQL.90%29.aspx

    Try to follow what’s on that page and see if it helps.
    Perhaps you did already and reported the IUSR_ bit?

    I think an important part mentioned is:
    It is important to notice that in most scenarios, this means that the Web server’s process identity or thread identity (if the Web server is using impersonation) is used to connect to the server, not an end-user’s identity.

    Would that mean that this user (IUSR_?) needs to be given permissions to the databases you’re using?

  16. August 6th, 2010 at 07:44

    Yes, I have used exactly what you had suggested and also the code in the link in your second message. In fact, that very same example is in the help file SQLServerDriverForPHP.chm you mentioned earlier.

    I have searched extensively on Google and MSDN, and tried all kinds of combinations. It seems this error message is common for Sharepoint installations, but their fixes have not worked for me – at least the ones that appear to apply.

    So still no juice.

  17. August 6th, 2010 at 08:50

    There is a web version of the SQL Server Driver documentation if it’s more convenient for you:
    http://msdn.microsoft.com/en-us/library/ee229551%28v=SQL.10%29.aspx

    I see. I don’t have experience with SharePoint so I don’t know how the database is set up and how users work. The SQL Server 2000 I have access to is a company installation that I don’t have admin rights to, so I can’t really authorize a Windows account for it.

    Do you have admin rights to your database server, or request rights? If so, what if you tried setting up a local database user and using SQL Server Authentication instead?

    By the way, is your PHP Server and your SQL Server on the same box, or separate?

  18. August 7th, 2010 at 07:39

    It worked! Let me qualify that. :-)

    As far as I can tell, Windows Authentication still does not work for me. However, I did create a new login on the SQL Server which used SQL Server Authentication and defaulted to the database I needed. I now get a connection!

    I have not tried any queries yet, but I am sure they will work – will report back. Also, I will let you know if Windows Auth works.

    Cheers!

  19. August 7th, 2010 at 23:20

    Great! Let me know how it goes, and if you get any further on Windows Auth.
    I took a shot yesterday at just leaving the user/pass out of the connection, and I got a similar error you got for login failed, except mine said that ‘(null)’ was not a valid user…

  20. August 8th, 2010 at 19:11

    I am happy to report that the SQL queries are working. However, I did have to change a bunch of function names from using the mssql_ prefix to the using the sqlsrv_prefix. e.g.
    mssql_fetch_array to sqlsrv_fetch_array.

    Never heard of a (null) user. Are you sure you were using Windows Authentication? Which Web Server. I am only familiar with IIS.

    Anyway thanx much for your help.

  21. August 8th, 2010 at 22:16

    I’m glad that everything is working out. Just to give you a heads up on something I came upon which gave me a huge headache and took a while to figure out. Using SQL Server 2000 and this SQL Server Driver, you cannot call another statement while actively in a statement. Here’s a quick example of what I’m talking about:

    $result = query('...');
    while ($row = fetch_array($result)) {
      $result2 = query('...');  // this will fail
      while ($line = fetch_array($result)) {
        // more code
      }
    }
    

    This is because SQL Server 2000 does not support Multiple Active Result Set (MARS), which is only available from 2005+. Do a search for more info. But if you come upon such a scenario, you’ll have to either open up another SQL connection, or save the results from the first statement, then run the second statement separately. I will write an article about it with more details, but I just want to give you a heads up since you have this combo. The old PHP 5.2 MSSQL extension apparently allowed us to do this nested statement without complaining. I’m not sure how, but I imagined it must have opened up an extra connection transparently.

    As for the null user, I just left out the username/password part of the connection array (as mentioned on the docs with using Windows Auth), and it said (null) was not a valid user. (And I’m sure there is no such user.) I use IIS6.0 and SQL Server 2000, like yours.

  22. August 9th, 2010 at 18:31

    That is funny. I was just about to sit down to do exactly what you have described above. I want to add records to a table, but first I want to check if a duplicate exists. I believe that above is the way I would do that. Anyway, opening up two DB connections seems like a good enough way to go.

    Are your IIS and SQL servers on different servers? That is the only scenario I can see that you would get a null user.

  23. August 9th, 2010 at 21:17

    I guess the route you choose depends on what you’re doing. I chose to separate the loop and save the results temporarily. Somehow I feel that opening up another connection just uses up more resource and memory. (I have no tests to back that case up.) If your application is public and will have many users, it might even hit that database connection limit quicker (whatever that limit may be). Mine’s an internal app, so I’m not really worried about it.

    Yep, my IIS and SQL Server are two different servers. I’m sure that’s why it thinks I’m trying to log in as a null user.

  24. August 10th, 2010 at 14:00

    Just want to mention that there is a newer version of the SQL Server Driver for PHP out (v2.0):
    http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=80e44913-24b4-4113-8807-caae6cf2ca05

    Just swapping out the DLL for the new one and restarting the web server should be enough, or if you want to rewrite it using the PDO option, that’s available too. (I have not tried out PDO.)

  25. August 13th, 2010 at 20:08

    Good post, will frequent your site.

  26. March 20th, 2012 at 03:22

    Hello,

    You wrote that (I quote): “PHP decided to remove the MS SQL Server connection driver in the PHP 5.3 package”. I am using IIS6, PHP 5.3 and Microsoft server 2003 along with MSSQL 2000. It doesn’t work for me. When installing wordpress, I always get the same error that no connection to database. Database name, user name or password can be wrong. I checked that more than once, everything is OK. Should I just change the PHP to 5.2? Is this the reason I can’t run WordPress? Would appreciate if someone helped me…

  27. March 20th, 2012 at 16:15

    Sam,

    WordPress uses a MySQL database. This article is about Microsoft SQL Server database. So that’s not the cause. You’ll need a MySQL database server to run WordPress.

  28. Guada
    May 30th, 2012 at 07:47

    I did everything you said, and i still get 404 error :(
    Dont know what to do, or what could it be, i downloaded php 5.4.3; i have SQL SERVER 2003 IIS 6

  29. Nana
    March 3rd, 2013 at 19:34

    hi..i’ve a problem with my sqlsrv_query..i’ve made a simple insert and update thru my application..my system popup insert successfull..but when i use mssql studio managemnt (MSSQL 2008 R2), there is no data in my table..i try to used back my query form my application and paste at my query editor at mssql studio managemnt , it’s works, 1 row(s) affected…i don’t now what is actually happenning…i’m using PHP 5.3.22, driver php_sqlsrv_53_nts_vc9.dll..i’ve no problem with my db connection..help me..