How to Connect SQL Server 2008 with PHP

OK! I just succeeded connecting PHP with SQL Server 2008. It’s not a fantastic thing for people though, but at least I did some research to make it. Now, let’s get into the point.

I use XAMPP 1.7.2 as a package for my web server application and it includes PHP 5 (thread safe version). Now, go to this Microsoft Download Center site to get all drivers needed for connection. When you’ve downloaded it, you just run it and it will show you a dialog and ask you a location where the files will be extracted. Just create a new folder in desktop and extract it there. After that, the first thing you should do is read SQLServerDriverForPHP_Readme.htm file, the most important part of the page is like this.

Readme File

You can see there are 2 kinds of php files, one with ‘ts’ and one without it. To check which one you use, just open PHP folder from C:\xampp\php and check whether it has php5.dll or php5ts.dll. Well, mine has php5ts.dll.

Next step is to determine which driver you should use, the one compiled in Visual C++ 9.0 (vc9) or in Visual C++ 6.0 (vc6). My web server application is compiled with VC++ 6.0 so I used php_sqlsrv_53_ts_vc6.dll. I got it when I restarted my web server application. First I used vc9 and when I restarted my server, it showed a warning that my driver is incompatible because the server is compiled in VC++ 6.00.

After that, you rename the file php_sqlsrv_53_ts_vc6.dll into php_sqlsrv_ts.dll and copy it into directory C:\xampp\php\ext. Next thing to do is opening your php.ini file and make some modification. Add this code :

extension=php_sqlsrv_ts.dll

and these lines of code

[sqlsrv]
sqlsrv.LogSubsystems = 1
sqlsrv.LogSeverity = 1
sqlsrv.WarningReturnAsErrors = 0

Well next is restart your web server application. And your driver is loaded and ready to be used.

To test the driver, first I create a database named Laboratorium and a table named tblUser with following columns :
username : varchar(255) – PK
password : varchar(255)
firstName : varchar(255)
lastName : varchar(255)

As dummy data, I inserted data :
username : hafiz
password : hafiz
firstName : Hafiz
lastName : Badrie Lubis

After that create a php file (index.php) contains this code :

//if you use Windows Authentication to establish a connection
$server = "(local)";
$connectionOptions = array("Database" => "Laboratorium");
$dbconnect = sqlsrv_connect($server, $connectionOptions) or die ("Problem on connecting to database");

$queryString = "SELECT * FROM tblUser";
$query = sqlsrv_query($dbconnect, $queryString) or die ("Problem in executing query");
while ($result = sqlsrv_fetch_array($query)) {
  echo $result['username'] . " " . $result['password'] . " " . $result['firstName'] . " " . $result['lastName'] . "";
}

The result of the code is : hafiz hafiz Hafiz Badrie Lubis

Enough from me, thank you.

Advertisements

19 thoughts on “How to Connect SQL Server 2008 with PHP

  1. Hi, I need to connect to my database in small business server 2008. I must add the username to connect to database. I use the server name and the database name but cannot connect. Where i must add the username. My small business server 2008 is named “B-sql” and sql server is named “(local)”. What i must write in $server = “(local)”;. Please help me and please if you have an answer send me to fiek2011@gmail.com

  2. As I am getting fowllowing error doing this to win server2K3 R2 Sp2, vc++9 php5.3.8

    Call to undefined function sqlsrv_connect() in line x

  3. Hafiz, I was looking for this solution. Thank you. I have few confusion
    1. Where we will add SQL server username and password in code.
    2. what if I have to select a “view” rather than a DB table.
    3. can I use this drive for appserv ?

    I shall be very thankful for helping me. You could email me at Gulraizahmed@gmail.com

  4. i have follow all of your steps but it didn’t work for my
    wen i restart my webserver(xampp version1.7.2) i get this warning Php startup unabel to load dynamiv library
    C:\xampp\php\ext\php_sqlsrv_ts.dll
    The specified module could not found

    and wen i execute the code i get this erro in my browser
    Fatal error: Call to undefined function sqlsrv_connect()
    i have google it for days but didn’t find any think .
    please can any body help my to fix this problem email mi in beni.-v@hotmail.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s