一、 Install the Microsoft PHP Drivers for SQL Server
[root@w91 source]#curl https://packages.microsoft.com/config/rhel/6/prod.repo > /etc/yum.repos.d/mssql-release.repo
[root@w91 source]# yum install -y msodbcsql mssql-tools unixODBC-devel
[root@w91 source]# yum -y install php-pear php7.0-dev
#下面如果安装失败下载后安装即可
[root@w91 source]# pecl install sqlsrv
[root@w91 source]# pecl install pdo_sqlsrv
#下载地址
[root@w91 source]# wget http://pecl.php.net/get/sqlsrv-5.2.0.tgz
[root@w91 source]# wget http://pecl.php.net/get/pdo_sqlsrv-5.3.0.tgz
执行到第二条时出现问题:
解决方式:
安装c++11 gcc4.8.x
[root@w91 home]wget http://people.centos.org/tru/devtools-2/devtools-2.repo -O /etc/yum.repos.d/devtools-2.repo
[root@w91 home]yum install devtoolset-2-gcc devtoolset-2-binutils devtoolset-2-gcc-c++
[root@w91 home]yum install devtoolset-2-gcc-gfortran
[root@w91 home]yum install devtoolset-2-binutils-devel
[root@w91 source]# ln -s /opt/rh/devtoolset-2/root/usr/bin/* /usr/local/bin/
[root@w91 source]# mv /usr/local/bin/sudo{,_bak}
[root@w91 source]# hash -r
[root@w91 source]#gcc --version
gcc (GCC) 4.8.2 20140120 (Red Hat 4.8.2-15)
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
再次执行
[root@w91 source]# pecl install sqlsrv
[root@w91 source]# pecl install pdo_sqlsrv
测试连接
$serverName = "192.168.10.20";
$connectionOptions = array(
"Database" => "db_fidfe",
"Uid" => "sea",
"PWD" => "12456M"
);
//Establishes the connection
$conn = sqlsrv_connect($serverName, $connectionOptions);
//Select Query
$tsql= "SELECT @@Version as SQL_VERSION";
//Executes the query
$getResults= sqlsrv_query($conn, $tsql);
//Error handling
if ($getResults == FALSE)
die(FormatErrors(sqlsrv_errors()));
?>
Results :
while ($row = sqlsrv_fetch_array($getResults, SQLSRV_FETCH_ASSOC)) {
echo ($row['SQL_VERSION']);
echo ("
");
}
sqlsrv_free_stmt($getResults);
function FormatErrors( $errors )
{
/* Display errors. */
echo "Error information:
";
foreach ( $errors as $error )
{
echo "SQLSTATE: ".$error['SQLSTATE']."
";
echo "Code: ".$error['code']."
";
echo "Message: ".$error['message']."
";
}
}
?>
附带网上两个centos6、7安装php7 的mssql扩展
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*