SQL Server Driver for PHP Setup Tutorial

Thursday, September 16th, 2010

So recently for work I had to work with some data in a Microsoft Access database. I imported this into SQL Server using the Microsoft Management Studio because the tools made my life easy. However my dev environment was already setup for php and I didn’t really feel like setting up an environment for ASP just to create some simple web pages for myself. I also wanted to avoid having to convert my db’s from Access to SQL Server and then import to MySQL. The answer is Microsoft’s opensource driver for PHP. This article will get you up and running in no time.

  1. Start by downloading Microsofts PHP Driver
  2. Extract it to a directory on your computer.
  3. Open a page that calls phpinfo() and look for the following information:
    • Version – 5.3 or 5.2 for example
    • Compiler – VC6 or VC9
    • Thread Safety – enabled or disabled
  4. Find the dll that in your extracted folder that matches the above information. For example sqlsrv_5.3_ts_vc6.dll would be for Version 5.3, Thread Safe Enabled and compiled in VC6.
  5. Copy the .dll into your php ext directory. For a WAMP installation this would typically be in c:\wamp\bin\php\phpversion\ext
  6. Enable the dll by either editing your php.ini file or from the WAMP taskbar icon
    • Click the taskbar icon -> restart all services  - the dll will show up in your extensions list
    • Click the taskbar icon again -> PHP -> PHP Extensions -> check php_sqlsrv_5.3_ts_vc6
    • Revisit your phpinfo() page and you should no have a section called sqlsrv
  7. Finally you just need to code up a page to grab some information from your sql database
$serverName = "(local)";
$connectionOptions = array("Database"=>"yoursqldb");

$conn = sqlsrv_connect( $serverName, $connectionOptions);

if( $conn === false )  { die( FormatErrors( sqlsrv_errors() ) ); } //terminate on failed connection

	$tsql = "SELECT TOP 100 [yourcolumn] FROM [yoursqldb]";
	$getRows = sqlsrv_query( $conn, $tsql);
	if ( $getRows === false) { die( FormatErrors( sqlsrv_errors() ) ); }

        //for each row output each column value
	while( $row = sqlsrv_fetch_array( $getRows, SQLSRV_FETCH_ASSOC)) {
		foreach ( $row as $key => $value ) {
			echo $value."<br />";

/* Free the statement and connection resources. */
sqlsrv_free_stmt( $getRows );
sqlsrv_close( $conn );

6 Responses to “SQL Server Driver for PHP Setup Tutorial”

  1. acjubz says:

    The site SQL Reports has a great SQL tutorial. Highly recommended for people just getting started on SQL selects.

  2. http://www.deskapahendri.com/2011/02/28/jasa-setting-mikrotik-dan-proxy-super-ngebut-24-jam/ says:

    Jasa Setting Mikrotik Jasa Setting Proxy

  3. I must thank you for the efforts you have put in writing this site. I really hope to check out the same high-grade content from you in the future as well. In truth, your creative writing abilities has motivated me to get my own, personal website now ;)

  4. Excellent beat ! I would like to apprentice while you amend your site,
    how could i subscribe for a blog website? The account helped me a acceptable deal.

    I had been tiny bit acquainted of this your broadcast offered
    bright clear idea

  5. serwis says:

    Hello to all, how is everything, I think every one is getting more from this site, and your views are pleasant in support of new viewers.

Leave a Reply