Linux - PHP 7.0 and Mssql (Microsoft SQL)

Unable to connect to MSSQL PHP7 with PDO: unixODBC

If your OS is UBUNTU

Install the unixODBC driver manager and Microsoft ODBC driver for Linux

wget https://raw.githubusercontent.com/Microsoft/msphpsql/PHP-7.0-Linux/ODBC%20install%20scripts/installodbc_ubuntu.sh

Run installer

sh installodbc_ubuntu.sh

Install PHP dependenceis

apt-get install php-pear php-dev

Install sqlsrv (check last version with pecl search sqlsrv)

pecl install sqlsrv-4.0.5

Install pdo_sqlsrv (check last version with pecl search sqlsrv)

pecl install pdo_sqlsrv-4.0.5

Load extensions

echo "extension=sqlsrv.so" | sudo tee --append /etc/php/7.0/fpm/php.ini
echo "extension=pdo_sqlsrv.so" | sudo tee --append /etc/php/7.0/fpm/php.ini

Restart PHP-FPM

service php7.0-fpm restart

For windows
https://learn.microsoft.com/en-us/sql/connect/php/loading-the-php-sql-driver?view=sql-server-2017

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*||"`

or via yum look; https://webtatic.com/packages/php70/

How to connect to SQL server using sqlsrv in CentOS 7.6?

Sorry, but using all examples from Microsoft SQL Server from PHP, I cannot reproduce your issue. Connection works.

  • check that server is ready to server client connections
  • check firewall configuration (at least 1433 should be allowed, check with netstat of used ports)
  • check from command line using sqlcmd command (from mssql-tools package)
  • check from command line using a simple PHP script
  • check SElinux configuration (httpd_can_network_connect)

P.S. notice I have push a small update of php-sqlsrv which properly pull msodbcsql17 instead of msodbcsql.

PHP 7.0/Yii 1/MS Sql Server 2008: PHP is unable to find the driver

Resolved with a simply ...

sudo apt install php7.0-sybase

No more configs to add, no restart required.

Found here: Linux - PHP 7.0 and MSSQL (Microsoft SQL) (but not is the accepted answer in that case)

MSSQL and PHP 7 (32 bits system)

This solved the problem for me:

sudo apt-get install php7.0-sybase

As I need UTF-8 encoding I had to change the version of the tds first:

sudo vim /etc/freetds/freetds.conf

Locate

[global]
# TDS protocol version
; tds version = VERSION_NUMBER

Set the current version number to be at least 7.0 and remove ; from the beginning of the line. In order to set UTF-8 global add:

client charset = UTF-8

below the version you just set.

Restart php-fpm:

sudo service restart php7.0-fpm restart

As I use Nginx I restarted that as well just to be sure.
Now try adding a mssql connection:

$this->connection = new PDO('dblib:host='.$this->hostname.':'.
$port.';dbname='.$this->db_name.';charset=UTF-8'
,$username, $password);

Note the charset=UTF-8 part. This can be removed, but if you don't set the client charset in the config file this is another place were you can set it.
If you change the charset in the connection string it will override the global value.

Greetings!



Related Topics



Leave a reply



Submit