Connect to MS SQL Server with PHP 5.3+

Date posted: 2013-03-22
Last updated: 2026-11-07

As of PHP 5.3.2 you have to use the SQLSRV API functions to connect to an Microsoft SQL Server database from PHP. For example, use sqlsrv_connect() to create a connection resource and open a connection. Learn how to connect to a Microsoft SQL Server database using PHP 5.3+ and SQLSRV.

Starting with PHP version 5.3.2, PHP dropped support for the MSSQL Functions. You now have to rely on a new driver created by Microsoft: SQLSRV. This article showcases how you connect to an SQL Server database with PHP.

In this article

PHP connect to SQL Server: Support for multiple PHP versions, List all table names in an SQL Server database - sqlsrv_query() show tables example, SQLSRV Driver API Reference

Conclusion connecting PHP 5.3+ to Microsoft SQL Server

As of PHP 5.3.2 you have to use the SQLSRV API functions to connect to an Microsoft SQL Server database from PHP. For example, use sqlsrv_connect() to create a connection resource and open a connection. Learn how to connect to a Microsoft SQL Server database using PHP 5.3+ and SQLSRV.

The main difference with the older mssql functions of PHP is that SQLSRV API requires an Array() with connection information, instead of strings.

Learn how to install SQLSRV driver and PHP extension on Windows Server.

PHP connect to SQL Server

An example you can use to connect from PHP to SQL Server with sqlsrv_connect() is:

<?php
/*
 * Follow me on Twitter: @Jan_Reilink
 * Donate: https://www.paypal.me/jreilink
 */

function connectMssql($hostname, $username, $dbname, $passwd) {
  $connectionInfo = array("UID" => $username, "PWD" => $passwd, "Database" => $dbname);
  $link = sqlsrv_connect($hostname, $connectionInfo);
  if ($link) {
    return TRUE;
  } else {
    return FALSE;
  }
}
 
if(connectMssql("sql.domain.tld", "user", "deebaase", "passw0rd")) {
  echo "We're connected!";
  // do stuff here, then close the SQL Server connection
  sqlsrv_close($link);
}
?>

Support for multiple PHP versions

Suppose you want to support both older and newer versions of PHP. You can use phpversion() to check the version you're currently using. Then use the SQL Server sqlsrv/mssql_connect() function for that specific version, like:

<?php
/*
 * Follow me on Twitter: @Jan_Reilink
 * Donate: https://www.paypal.me/jreilink
 *
 * Pseudo code for illustration only
 */

function connectMssql($hostname, $username, $dbname, $passwd) {
  if(strnatcmp(phpversion(),"5.3.2") >= 0) {
    /*
     * PHP versie 5.3.2 or higher needs sqlsrv-driver:
     * http://msdn.microsoft.com/en-us/library/cc296161%28SQL.90%29.aspx
     */

    $connectionInfo = array("UID" => $username, "PWD" => $passwd, "Database"=> $dbname);
    $link = sqlsrv_connect($hostname, $connectionInfo);
    if ($link) {
      return TRUE;
      sqlsrv_close($link);
      exit;
    }
  }

  else {
    $link = mssql_connect($hostname, $username, $passwd);
    if ((!$link) || (!mssql_select_db($dbname, $link))) {
      return FALSE;
    }
    else { return TRUE; }
    if ($link) { mssql_close($link); }
  }
}

// execute the function
connectMssql("sql.domain.tld", "user", "deebaase", "passw0rd");
?>

List all table names in an SQL Server database - sqlsrv_query() show tables example

<?php
/*
 * Follow me on Twitter: @Jan_Reilink
 * Donate: https://www.paypal.me/jreilink
 *
 */

$connectionInfo = array("UID" => "db-user", "PWD" => "passw0rd", "Database" => "deebaase");
$link = sqlsrv_connect("sql.domain.tld", $connectionInfo);
$sql = "SELECT table_name FROM information_schema.tables";

$stmt = sqlsrv_query( $link, $sql);
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
  echo $row['table_name']."<br />";
}

if( $stmt === false ) {
  die( print_r( sqlsrv_errors(), true));
}
?>

sqlsrv_query - Prepares and executes a query.

SQLSRV Driver API Reference

You can find more information on the SQLSRV API on Microsofts MSDN website: SQLSRV Driver API Reference (Microsoft Drivers for PHP for SQL Server)

Conclusion connecting PHP 5.3+ to Microsoft SQL Server

Used version numbers in this post might be outdated, but the post may still hold valuable information.

In this article you learned you no longer can use mssql_connect() in PHP 5.3 and up. You have to use a newer driver and API called SQLSRV with, for example, sqlsrv_connect() function. The main difference with the older mssql functions of PHP is that SQLSRV requires an array with connection information instead of strings.

This post was most recently updated on .

QR code: scan to donate 5 euro via PayPalScan to donate €5 via PayPalI write these posts in my spare time, based on real problems from my day job as a sysadmin. If this one saved you some debugging time, a 5 euro donation via PayPal is much appreciated, or link to it from your own site. Thanks! 🙏

This post is part of my PHP guides.

Leave a Comment