web development
This is something that a number of people I know have struggled with, so I hope to help out with what I have learned.
To connect your PHP program with MySQL is probably step two for any PHP developer and it’s very easy. In principle, connecting with SQL Server (Microsoft SQL) should also be easy, but when most people have their first go at a MSSQL connection, they just get the following:
SQLSTATE[01002] unable to connect:
sql server is unavailable or does not exist.
The thing is, PHP and it’s MySQL buddy are both free and they like each other, so if you install a new Apache server (or similar) its already set up to run MySQL connections without problem, but it’s not the same for MSSQL.
Here is a list of things you need to change in your setup to be able to open a MSSQL connection with PHP:
Firstly, you need to configure your php.ini file (Apache Folder/bin/php.ini).
Find the line that says
mssql.secure_connection = Off
and change it to
mssql.secure_connection = On
Now enable the following extensions (you do this by deleting the ; in front of the line):
extension=php_mssql.dll
extension=php_sqlite.dll
And for those using PDO (as I do)
extension=php_pdo.dll
extension=php_pdo_sqlite.dll
extension=php_pdo_mssql.dll
extension=php_pdo_odbc.dll
Secondly, PHP 5.2x has a file that is buggy — NTWDBLIB.DLL — which needs to be replaced with a newer version. You can download it here: http://www.dlldll.com/ntwdblib.dll_download.html.
Keep it in your C:/Windows/System32/ folder, but also search for copies of the file in your Apache and PHP setup/settings folders, and replace them as well.
Lastly, make shure that on your SQL Server you alter the login’s default to ‘master’:
alter login {loginname} with default_database = ‘master’
That ought to sort it out! If I have left something out however, please submit a comment so that I can update the post — unfortunately I didn’t make notes while I was battling with the setup.
PLease note: this setup was done on a windows system running apache. If you want more info on setting it up on a linux machine here is a good article: php-access-to-an-mssql-database-from-debian-lenny-with-odbc-and-freetds
Based in South Africa, we're a web-development company...
(3)
(1)
(5)
(4)
(1)
(14)
(1)
(8)
(7)
(17)
(4)
(2)
(2)
(1)
3 Responses to SQL Server (MSSQL) pre-connection setup in PHP
Razvan
June 28th, 2010 at 8:34 pm
Great tip with ntwdblib.dll. Thanks for sharing
Khawar
January 19th, 2011 at 5:23 am
Great help boss. May God Bless you…
Robin Michael
November 29th, 2011 at 2:10 pm
It is Perfect article for buggy ntwdblib.dll.
I found this such a nice article after 6 hrs hunt
MR.NICO You are really great!!!
Thanks a lot
Robin M.