Linux - PHP 7.0 and MSSQL (Microsoft SQL)

Linux - PHP 7.0 and MSSQL (Microsoft SQL)

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The Shift from FreeTDS to ODBC in PHP 7.0 on Linux

When PHP 7.0 was released, it introduced a significant architectural shift that broke backward compatibility for many enterprise scenarios. Specifically, the extension required to connect to Microsoft SQL Server (MSSQL) from a Linux environment was removed. Prior to this release, developers relying on FreeTDS could establish connections using the mssql_* functions. However, with those extensions gone, the landscape for connecting Linux servers to MSSQL changed drastically.

The core issue lies in how PHP communicates with databases. On Windows, the native ODBC driver (sqlsrv) often handles the heavy lifting seamlessly. But on Linux, the ecosystem historically relied on FreeTDS as a bridge between the C extension and the Microsoft SQL Server protocol. When PHP 7 dropped support for the mssql extension and the underlying FreeTDS library integration, it didn't just remove a tool; it removed the standard path for many existing deployments.

This isn't necessarily a "slap in the face" to developers, but rather a push toward a more standardized interface: ODBC. In modern Linux environments, Microsoft no longer provides official ODBC drivers, and FreeTDS is effectively deprecated for this specific use case in newer PHP versions. The solution that has emerged as the de facto standard is leveraging the pdo_odbc extension alongside a third-party ODBC driver manager like unixODBC or iODBC.

To get working code with MSSQL 2012 and PHP 7.0 on Linux, you must install a compatible ODBC driver. The most common approach involves using the Microsoft ODBC Driver for SQL Server, which is available as a binary package for various Linux distributions. You will need to download the appropriate .deb or .rpm package from Microsoft's official sources and install it alongside unixODBC.

Once the driver is installed, you must configure the ODBC Data Source Name (DSN). This usually involves editing configuration files located in /etc/odbc.ini or creating a new DSN using the isql command-line tool. A typical configuration block might look like this:

[MSQLServer2012]
Driver = /usr/lib/x86_64-linux-gnu/odbc/msodbcsql.so
Description = MSSQL Server 2012 Connection
Database = YourDatabaseName
ServerAddress = your-sql-server-hostname
Port = 1433
Username = your_username
Password = your_password
ConnectionTimeout = 15

After defining the DSN, you can connect from PHP using PDO. Note that you must explicitly load the pdo_odbc extension in your php.ini, as it is not always enabled by default:

<?php
$dsn = 'ODBC:DSN=MSQLServer2012';
$username = 'your_username';
$password = 'your_password';

try {
    $pdo = new PDO($dsn, $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    // Example query
    $stmt = $pdo->query("SELECT * FROM Employees");
    print_r($stmt->fetchAll());
} catch (PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}

There are distinct trade-offs to consider with this approach. The performance of pdo_odbc can sometimes lag behind the native sqlsrv extension found on Windows, particularly in high-concurrency environments. Additionally, driver installation on Linux is notoriously distribution-specific; what works on CentOS 7 might require different package management steps than Ubuntu 18.04.

Another critical pitfall is the handling of authentication. Older FreeTDS setups often relied on trusted connections or specific security contexts that are harder to replicate with modern ODBC drivers without careful configuration of the Trusted_Connection parameter in the DSN. If you are maintaining a legacy application, migrating the connection logic from mssql_connect to PDO requires careful refactoring to ensure error handling and transaction management remain robust.

While the transition feels abrupt compared to the seamless experience of PHP 5.x, it aligns Linux development more closely with modern database connectivity standards. For enterprise shops running MSSQL on Linux, the path forward involves accepting pdo_odbc as the primary interface, ensuring your ODBC drivers are kept up to date, and adjusting expectations regarding slight overhead compared to native Windows drivers.

Our network