PHP with Microsoft IIS and Microsoft SQL server
March 20, 2008 – 7:23 am Tags: Databases, PHPYou often hear that using PHP under Microsoft servers is a bad idea, because it doesn’t work very well.
But is that even true? You guessed it… no. It is actually possible to get a PHP based database application working under IIS and MS SQL Server perfectly fine, it just may take some configuration.
You can even get Apache mod_rewrite type of functionality under IIS with Isapi_Rewrite.
Why use IIS/MSSQL?
There’s a myriad of reasons for using IIS and MSSQL instead of the more traditional and proven Apache+MySQL combination for running PHP.
- ASP.NET - You already have ASP or ASP.NET based applications
- Familiarity - You’re more familiar with Microsoft products
- etc.
There are also some advantages, like IIS’s caching features which can cache even dynamic pages, like the ones produced by PHP. You can also combine ASP pages and PHP pages under the same site.
Getting everything running
Nowadays you get PHP with FastCGI bundled with IIS 7, so getting PHP working with it is a matter of putting PHP files in your web root. With IIS 6, you’ll need to install PHP manually. You can find the official FastCGI extension and PHP for IIS 6 from Microsoft’s official IIS site.
In case your PHP files aren’t executed, you should check that PHP files are associated with the correct handler. At least with IIS 7, you’ll want to associate *.php with the PHP - FastCGI handler.
Connecting to the MS SQL database should work out of the box at least with PDO.
mod_rewrite with Isapi_Rewrite
For Apache’s mod_rewrite style functionality, you can use the Isapi_Rewrite module. The full version priced at $99 is reasonable, but you can also get the slightly limited Lite-version which is a free download.
The installation should be as simple as running the MSI installer. After that, be sure to read the documentation on the site. Note that for the Lite-version, you can only have one htaccess file, which affects your server globally.
With the Lite-version, since you’re limited to one htaccess file, you will have to work a bit differently. If you want to rewrite an URL under yoursite/example/ to yoursite/example/index.php, you’ll have to remember that the path will need to contain /example/ in it, because it’s only caught globally. If you have the full version, you should be able to just put a htaccess file under your example directory and use it like Apache’s .htaccess.
Possible compatibility issues with PHP
You should note that under IIS, you will get some different $_SERVER variables. This may cause some issues if you’ve used Apache-specific ones in your PHP code.
Another thing to note is that PHP’s short_open_tags feature is disabled by default, at least with the IIS 7 bundled PHP. This will effectively disable the = ... ?> tags, which are often used in Zend Framework view scripts for example. You may have to manually enable it, so if you use short tags, remember to check php.ini
Differences between MS SQL and MySQL
If you are not very familiar with MS SQL, you may run into several annoying and possibly confusing problems. Some database admin tools, such as Zend Studio, will run CREATE TABLE queries so that the table owner will not be dbo, and will instead use your login name. This may cause problems in statements, since if the owner is not dbo, you will have to prefix the owner’s name in the statement, like “SELECT id FROM Myuser.exampletable”.
An excellent reference for MS SQL’s Transact-SQL syntax can be found at MSDN’s Transact-SQL manual.

One Response to “PHP with Microsoft IIS and Microsoft SQL server”
Also, we must not forget support, documentation, usability, easy (and successful) installation that usually come with MS products. May be that was not the case 10 years ago, but today MS platform has a lot more advantages than others.
Additionally, IIS 7 has now a flexible configuration schema and a super powerful extensibility API. The product is new and there is little experience about it yet, but there will soon be a plethora of amazing plug-ins for this webserver.
PHP surely works just fine on IIS. As for PHP apps on IIS, until recently the only problem was the absense of mod_rewrite. Today there are many rewritting tools for IIS, some compatible with mod_rewrite and some with completely different usage. I will also mention IIS Mod-Rewrite ( http://www.micronovae.com/ModRewrite/ModRewrite.html ) which is a solid mod_rewrite port for IIS and solves SEF issues with no pain at all. More URL rewriting solutions at http://en.wikipedia.org/wiki/Rewrite_engine … Oh, did I say that IIS has many more rewriting solutions than Apache???
Of course, there is no comparison between MS SQL Server and MySQL. MS SQL beats MySQL at almost all points. Dynamic cursors, better drivers (ODBC), a lot better administration UI, rock solid core, high workload efficiency with high level of parallelism, and a lot more. And you can get MS SQL Express for free and run databases of up to 4GB.
Of course all these capabilities and facilities have a price. But for serious IT projects the cost of MS software is pennies compare to the saved labor costs for development and administration.
By Craig on Mar 21, 2008