Issue
Some time you may facing the following error when trying to connect to MS SQL Server from PHP using the sqlsrv_connect() function:
Error As follows: Array ( [0] => Array ( [0] => IMSSP [SQLSTATE] => IMSSP [1] => -49 [code] => -49 [2] =>
This extension requires the Microsoft ODBC Driver for SQL Server.
Access the following URL to download the ODBC Driver for SQL Server for x64: https://go.microsoft.com/fwlink/?LinkId=163712 [message]
=> This extension requires the Microsoft ODBC Driver for SQL Server.
Access the following URL to download the ODBC Driver for SQL Server for x64: https://go.microsoft.com/fwlink/?LinkId=163712 ) )
Environment
- CloudLinux 6/7/8
Solution
1. To configure ODBC, please perform the following actions:
/opt/alt/alt-php-config/install_odbc
yum install alt-freetds*
Verify that it was installed properly:
odbcinst -q -d
1.1 In the /etc/odbcinst.ini
, add details about FreeTDS driver:
[FreeTDS]
Description=FreeTDS unixODBC Driver
Driver=/opt/alt/freetds/usr/lib64/libtdsodbc.so.0
Setup=/opt/alt/freetds/usr/lib64/libtdsodbc.so.0
UsageCount=1
[SQL Server]
Description=FreeTDS unixODBC Driver
Driver=/opt/alt/freetds/usr/lib64/libtdsodbc.so.0
Setup=/opt/alt/freetds/usr/lib64/libtdsodbc.so.0
UsageCount=2
* For i386/i686 lib64 use lib in path
1.2 Add details about the MSSQL server to /etc/odbc.ini
:
Description = MSSQL Server
Driver = FreeTDS //should have the same name as driver in /etc/odbcinst.ini
Database = %DB_NAME%
Server = %IP%
Port = %PORT%
1.3 Update CageFS:
cagefsctl --update-etc
1.4 Enable PHP extensions:selectorctl -u %USERNAME% -v %PHP_VERSION% -e pdo_odbc,odbc
1.5 Add the following syntax in the connection script:
$pdo = new PDO(
sprintf(
"sqlsrv:server=%s;database=%s",
"sqlserver-hostname,port",
"database"
),
"user",
"password"
);
1.6 Also, you need to run the following commands:
/opt/alt/alt-php-config/install_odbc
yum install unixODBC
yum install unixODBC-devel
cagefsctl --addrpm unixODBC
cagefsctl --force-update
cagefsctl -M
So basically, you need the following PHP extensions:
pdo_odbc,odbc
To enable them, please run the following command:
selectorctl --enable-user-extensions=pdo_dblib,odbc --version=X.X --user=UserName
where x.x depends on PHP version which you use for the website.
Comments
0 comments
Please sign in to leave a comment.