// article

Howtos

MS SQL Server 2008 with PHP

I personally choose not to use Micro$oft products when it is possible, but sometimes the choice is not given. For these unfortunate who have to deal with SQL Server 2008 (or 2005), here is how to get around without too many swear words and avoid licensing costs.

Requirements

  • Windows Vista (Win XP should do also)
  • .Net Framework 3.5 SP1
  • Windows Installer 4.5
  • Windows PowerShell 1.0
  • MS Core XML Services (MSXML) 6.0

Note: You need to have genuine windows copy to download PowerShell from Microsoft website; however it is possible to find it elsewhere if you google around.

Setup SQL Server

  1. Download and install MS SQL Server Express 2008 with Advanced Services and choose the following options:
  2. Single instance Install (not clustered !)
  3. Name the instance “SQLExpress”
  4. In the options box choose “SQL Server”, and “Management Studio” (do not install full-text search, as it requires clustering)
  5. Set the Server Service Account Name to “NT AUTHORITY\NETWORK SERVICE”, auto
  6. Set the SQL Server Browser to “NT AUTHORITY\NETWORK SERVICE”, manual
  7. Choose mixed mode authentication
  8. Set SQL Server to listen on TCP port.
    1. Start “SQL Server Configuration Manager”
    2. Network Configuration -> Protocols : enable “Named Pipes” and “TCP/IP”
    3. Right-click “TCP/IP” -> Listen All = Yes”
    4. In tab “IP addresses” -> Section IPAll -> TCP Dynamic Ports = 1433
    5. Repeat these steps for SQL Native Client.
    6. Restart SQL Server
  9. Create your database and set the owner to user ‘sa’, and change the user sa password.

Setup PHP

This assumes you have wamp installed, otherwise just change the path.

  1. Download the good version of ntwdblib.dll (2000.80.194.0)
  2. Overwrite C:\wamp\bin\apache\apache2.2.8\bin\ntwdblib.dll
  3. Overwrite C:\wamp\bin\php\php5.2.6\ntwdblib.dll
  4. Enable php_mssql.dll in php.ini
  5. Restart Apache
    1
    2
    3
    $server = 'BANGKOK\SQLEXPRESS';  // The format is "HOST\SERVERNAME[,PORT]"
    $username = 'sa';
    mssql_connect($server, $username, $password);

Debugging

  • You can test if SQL Server is listening on port 1433 by using NMap/Zenmap for Windows to do an “Intense scan, all TCP ports” on localhost, and you should see port 1433 open.
  • When trying to log within PHP you should see an entry in the log file:
    c:\program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\Log\ERRORLOG

I hope this will save you some time and swears!

 

Discussion

14 Responses to “MS SQL Server 2008 with PHP”

  1. It’s works perfectly!! Thanks for your help.

    Posted by Willy Rosal | June 25, 2011, 9:11 pm
  2. After looking around for answers about this topic, this is the most helpful post I found! Clear, simple and well written! Thank you very much David!. you saved the day at my office!! :D

    Posted by Johann | June 25, 2011, 9:11 pm
  3. Wow! Perfect looked for a solution for more than a day! Now it worked! Thanks!

    Posted by David Stein | June 25, 2011, 9:11 pm
  4. It works very well!Merci

    Posted by gwada | June 25, 2011, 9:12 pm
  5. i have error like this,what should i do ,please help me Warning: mssql_connect() [function.mssql-connect]: message: The USE database statement failed because the database collation Persian_100_CI_AS is not recognized by older client drivers. Try upgrading the client operating system or applying a service update to the database client software, or use a different collation. See SQL Server Books Online for more information on changing collations. (severity 16) in C:\wamp\www\tabligh\index.php on line 29 Warning: mssql_connect() [function.mssql-connect]: message: Login failed for user ‘sa’. (severity 14) in C:\wamp\www\tabligh\index.php on line 29 Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: localhost in C:\wamp\www\tabligh\index.php on line 29 Warning: mssql_select_db(): supplied argument is not a valid MS SQL-Link resource in C:\wamp\www\tabligh\index.php on line 32 Warning: mssql_query() [function.mssql-query]: message: Login failed for user ”. (severity 14) in C:\wamp\www\tabligh\index.php on line 38 Warning: mssql_query() [function.mssql-query]: Unable to connect to server: (null) in C:\wamp\www\tabligh\index.php on line 38 Warning: mssql_query() [function.mssql-query]: A link to the server could not be established in C:\wamp\www\tabligh\index.php on line 38 Warning: mssql_fetch_array(): supplied argument is not a valid MS SQL-result resource in C:\wamp\www\tabligh\index.php on line 41 Warning: mssql_close(): supplied argument is not a valid MS SQL-Link resource in C:\wamp\www\tabligh\index.php on line 47

    Posted by masoud | June 25, 2011, 9:12 pm
  6. Have you tried with a different encoding supporting the persian ? You can change this on your database using: ALTER DATABASE MyDatabase COLLATE I don’t know persian but you should be fine with UTF-8, make backup first.

    Posted by David | June 25, 2011, 9:14 pm
  7. Very good, this save the life of my project manager.

    Posted by alex | June 25, 2011, 9:14 pm
  8. thanks, it’s work

    Posted by fugu | June 25, 2011, 9:15 pm
  9. it’s work for me..thanks a lot

    Posted by khamam | October 17, 2011, 4:00 am
  10. Porque não utilizar PDO ?

    Posted by Marcos Paulo | November 9, 2011, 9:38 am
  11. ok, in my case, “Overwrite C:\wamp\bin\apache\apache2.2.8\bin\ntwdblib.dll”, thank’s!

    Posted by JC | February 23, 2012, 7:45 pm
  12. i had php5.4.3 and apache 2.2.22 and did as u said exactly but the same issue……..fatal error: undefined function mssql_connect()…….plz help i had wasted 48 hours on it……:(

    Posted by ahsan | November 14, 2012, 9:38 am

Trackbacks/Pingbacks

  1. [...] Well, after a few hours of searching, I found this blog: http://www.davidlauzon.net/2009/02/ms-sql-server-2008-with-php/ [...]

Post a comment