The SQLSRV extension allows you to access Microsoft SQL Server and SQL Azure databases when running PHP on Windows. The 3.0 release of the driver supports SQL Server, beginning with SQL Server 2005, including SQL Server 2012 and SQL Server 2012 LocalDB.

Follow this easy steps to install and connect MSSQL with sqlsrv and php :

Remove all unixODBC packages before proceding

yum remove unixODBC*

# CentOS 6

curl https://packages.microsoft.com/config/rhel/6/prod.repo > /etc/yum.repos.d/mssql-release.repo

# CentOS 7

curl https://packages.microsoft.com/config/rhel/7/prod.repo > /etc/yum.repos.d/mssql-release.repo

# Installing Microsoft ODBC Driver :
This packages provides an ODBC driver that can connect to Microsoft SQL Server.

ACCEPT_EULA=Y yum install msodbcsql17 unixODBC unixODBC-devel

# After the above installation go to php switcher/selector/php-fpm selector and build the php 7.xx with sqlsrv, you can enable sqlsrv from PHP "Options"

TO check the connection to MSSQL use this php script :

<?php
$serverName = "your server hostname/IP, port";
$connectionOptions = array(
    "database" => "yourDatabase",
    "uid" => "yourUsername",
    "pwd" => "yourPassword"
);

// Establishes the connection
$conn = sqlsrv_connect($serverName, $connectionOptions);
if ($conn === false) {
    die(formatErrors(sqlsrv_errors()));
}

// Select Query
$tsql = "SELECT @@Version AS SQL_VERSION";

// Executes the query
$stmt = sqlsrv_query($conn, $tsql);

// Error handling
if ($stmt === false) {
    die(formatErrors(sqlsrv_errors()));
}
?>

<h1> Results : </h1>

<?php
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
    echo $row['SQL_VERSION'] . PHP_EOL;
}

sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);

function formatErrors($errors)
{
    // Display errors
    echo "Error information: <br/>";
    foreach ($errors as $error) {
        echo "SQLSTATE: ". $error['SQLSTATE'] . "<br/>";
        echo "Code: ". $error['code'] . "<br/>";
        echo "Message: ". $error['message'] . "<br/>";
    }
}
?>

 

change :
serverName = "your server hostname/IP, port"
"database" => "Database name"
"uid" => "yourUsername"
"pwd" => "yourPassword

you'll see important information for MSSQL server like version and all.


Old Instructions

OLD Manual Instructions for installation of MsSQL extension for PHP 7.
User this only if above version fails.

CentOS 6

curl https://packages.microsoft.com/config/rhel/6/prod.repo > /etc/yum.repos.d/mssql-release.repo
yum clean all
yum update -y
ACCEPT_EULA=Y yum install -y msodbcsql mssql-tools unixODBC-devel 
cd /usr/local/src
rm -rf sqlsrv* pdo*
wget https://pecl.php.net/get/sqlsrv-4.3.0.tgz
tar -zxvf sqlsrv-4.3.0.tgz
cd sqlsrv-4.3.0
phpize
./configure
make
make install
echo ""
grep "sqlsrv.so" /usr/local/php/php.ini || echo "extension=sqlsrv.so" >> /usr/local/php/php.ini
cd /usr/local/src
wget https://pecl.php.net/get/pdo_sqlsrv-4.3.0.tgz
tar -zxvf pdo_sqlsrv-4.3.0.tgz
cd pdo_sqlsrv-4.3.0
phpize
./configure
make
make install
echo ""
grep "pdo_sqlsrv.so" /usr/local/php/php.ini || echo "extension=pdo_sqlsrv.so" >> /usr/local/php/php.ini
rm -rf sqlsrv* pdo*

CentOS 7

curl https://packages.microsoft.com/config/rhel/7/prod.repo > /etc/yum.repos.d/mssql-release.repo
yum clean all
yum update -y
ACCEPT_EULA=Y yum install -y msodbcsql mssql-tools unixODBC-devel 
cd /usr/local/src
rm -rf sqlsrv* pdo*
wget https://pecl.php.net/get/sqlsrv-4.3.0.tgz
tar -zxvf sqlsrv-4.3.0.tgz
cd sqlsrv-4.3.0
phpize
./configure
make
make install
echo ""
grep "sqlsrv.so" /usr/local/php/php.ini || echo "extension=sqlsrv.so" >> /usr/local/php/php.ini
cd /usr/local/src
wget https://pecl.php.net/get/pdo_sqlsrv-4.3.0.tgz
tar -zxvf pdo_sqlsrv-4.3.0.tgz
cd pdo_sqlsrv-4.3.0
phpize
./configure
make
make install
echo ""
grep "pdo_sqlsrv.so" /usr/local/php/php.ini || echo "extension=pdo_sqlsrv.so" >> /usr/local/php/php.ini
rm -rf sqlsrv* pdo*

Tagged: