Linux - PHP 7.0 and MSSQL (Microsoft SQL)

二次信任 提交于 2019-11-27 11:07:01

Microsoft has PHP Linux Drivers for SQL Server for PHP 7 and above on PECL. These are production ready. To download them, follow these steps:

Ubuntu 16.04:

sudo su 
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
exit
sudo apt-get update
sudo ACCEPT_EULA=Y apt-get install -y msodbcsql mssql-tools unixodbc-dev
sudo pecl install sqlsrv
sudo pecl install pdo_sqlsrv
echo "extension=sqlsrv" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`
echo "extension=pdo_sqlsrv" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`

CentOS 7:

sudo su
curl https://packages.microsoft.com/config/rhel/7/prod.repo > /etc/yum.repos.d/mssql-release.repo
exit
sudo yum update
sudo ACCEPT_EULA=Y yum install -y msodbcsql mssql-tools unixODBC-devel 
sudo yum groupinstall "Development Tools"
sudo pecl install sqlsrv
sudo pecl install pdo_sqlsrv
echo "extension=sqlsrv" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`
echo "extension=pdo_sqlsrv" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`

This will install the PHP SQL Server Drivers and register them in the php.ini folder.

Verify that it works by using the following sample

<?php
$serverName = "localhost";
$connectionOptions = array(
    "Database" => "SampleDB",
    "Uid" => "sa",
    "PWD" => "your_password"
);
//Establishes the connection
$conn = sqlsrv_connect($serverName, $connectionOptions);
if($conn)
    echo "Connected!"
?>

Links for reference:

shrty

The sybase of PHP7 contains the pdo_dblib module.

sudo apt install php7.0-sybase

tldr; sqlsrv and pdo_sqlsrv php extentions were very slow with large queries with lots of parameters, but installing and using pdo-dblib resolved the issue for me.

Running on php framework laravel 5.1 and 5.6 (on php 7.1 and 7.2) on Ubunutu 16.04. I found that the packages sqlsrv and pdo_sqlsrv did not work well for large queries. I had a large query with 30 bound variables. Sql Server 2008 converted all of the bound variables to nvarchar(4000) causing the db to do loads of casting taking forever.

I then disable the sqlsrv.so and pdo_sqlsrv.so extentions and installed pdo-dblib extension with:

sudo apt-get install php7.2-pdo-dblib

Then the query processed much quicker.

For more information:

Under the hood laravel uses a PDOStatement like this:

$conn = new PDO( "dblib:host=$host:1433;dbname=$db;", $uid, $pwd);
$stmt = $conn->prepare( $query );
$stmt->execute($param);

where a direct query like

$conn = new PDO( "dblib:host=$host:1433;dbname=$db;", $uid, $pwd);
$results = $conn->query( $query_with_parameter_already_bound );

would work fine.

Official MS extension has branch for PHP 7:

There's still lot of things missing, some marked as planned (Linux support is amongst them), nevertheless it could be another solution in the future.

EDIT (09-09-2016): There were already few Linux releases published since March, with CentOS/Ubuntu specific packages and source available. Keep in mind they aren't marked as Production Ready yet.

I definitely agree with you. I work primarily with SQL Servers at work and do not understand why they are not including default drivers for SQL servers in PHP.

For linux, i'm not too sure what you previously used but I found that the "dblib" driver is the best driver to connect to SQL Servers.

But basically for a linux box you just want to run these few steps to have a sql server driver installed.

apt-get install freetds-dev -y
vim /etc/freetds.conf

Then go ahead and add your connections there and restart apache and you should be good to go!

As per the answer above - the steps output a shared object (*.so) so the php.ini file needs the file extension too.

echo "extension=sqlsrv.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`
echo "extension=pdo_sqlsrv.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`

I modified

yum install freetds-dev
vim /etc/freetds.conf

And modify freetds.conf, install php mssql module

yum install php-mssql.x86_64

A short dump for the Debian people:

# Install MSSQL Client for PHP7 on Debian 9
apt update && apt install curl apt-transport-https
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
curl https://packages.microsoft.com/config/debian/9/prod.list > /etc/apt/sources.list.d/mssql-release.list
apt update
ACCEPT_EULA=Y apt install msodbcsql17 mssql-tools unixodbc-dev php-pear php7.0-dev
pecl install sqlsrv
pecl install pdo_sqlsrv
# if previous pecl cmds fails to download files at it's own - try the following ones:
#wget http://pecl.php.net/get/sqlsrv-5.2.0.tgz
#wget http://pecl.php.net/get/pdo_sqlsrv-5.2.0.tgz
#pecl install sqlsrv-5.2.0.tgz
#pecl install pdo_sqlsrv-5.2.0.tgz

cat <<EOF > /etc/php/7.0/cli/conf.d/99-sqlsrv.ini  
extension=sqlsrv.so
extension=pdo_sqlsrv.so
EOF
cat <<EOF > /etc/php/7.0/apache2/conf.d/99-sqlsrv.ini  
extension=sqlsrv.so
extension=pdo_sqlsrv.so
EOF

service apache2 restart
chacha kairu
sudo apt-get install php7.2-pdo-dblib

this worked for me on PHP 7.0 but still, you need to add the extension on

/etc/php/7.0/apache2/php.ini

as well as

/etc/php/7.0/cli/php.ini

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!