最後更新: 2024-10-03
介紹
These drivers rely on the Microsoft ODBC Driver for SQL Server to handle the low-level communication
Info
Prebuilt binaries
# from the Github project page
# Ubuntu16~18, Centos7 ...
https://github.com/Microsoft/msphpsql/releases
支援(@2024-10)
- PHP: 8.1.x, 8.2.x, 8.3.x
- for accessing data in all editions of SQL Server 2012 and later (including Azure SQL DB)
Version
- 5.12.0 (2024-01-31)
- 5.11.1 (2023-09-07)
- 5.11.0 (2023-03-07)
- ...
Compile From Source Code
# to avoid conflicts
yum remove unixODBC-utf16 unixODBC-utf16-devel
# Prerequisites
yum install php70-php-devel
# Install the ODBC driver for Centos
curl https://packages.microsoft.com/config/rhel/6/prod.repo > \
/etc/yum.repos.d/mssql-release.repo
# msodbcsql 有關 Package
ACCEPT_EULA=Y yum install msodbcsql17
# unixODBC development headers
yum install unixODBC-devel
# optional: for bcp and sqlcmd
ACCEPT_EULA=Y yum install mssql-tools
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
source ~/.bashrc
Get Source & Compile
# sqlsrv
wget https://pecl.php.net/get/sqlsrv-5.3.0.tgz # pdo_sqlsrv 依賴 sqlsrv
tar xvzf sqlsrv-5.3.0.tgz
cd sqlsrv-5.3.0/
phpize
./configure --with-php-config=/usr/bin/php-config
make
make install
# pdo_sqlsrv
wget https://pecl.php.net/get/pdo_sqlsrv-5.3.0.tgz
tar xvzf pdo_sqlsrv-5.3.0.tgz
cd pdo_sqlsrv-5.3.0
phpize
./configure --with-php-config=/usr/bin/php-config
make
make install
# Checking
ls /usr/lib64/php/modules/*sqlsrv*
/usr/lib64/php/modules/pdo_sqlsrv.so /usr/lib64/php/modules/sqlsrv.so
Troubleshoot
[1]
Error
cc1plus: error: unrecognized command line option "-std=c++11" cc1plus: error: unrecognized command line option "-std=c++11" make: *** [conn.lo] Error 1
原因: requires a more recent GCC than the default
rpm -qa | grep gcc
libgcc-4.4.7-23.el6.x86_64 gcc-4.4.7-23.el6.x86_64 gcc-c++-4.4.7-23.el6.x86_64
解決
https://datahunter.org/scl#devtoolset-7
[2]
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/pdo_sqlsrv.so' - /usr/lib64/php/modules/pdo_sqlsrv.so: undefined symbol: php_pdo_register_driver in Unknown on line 0
[1] 沒有安 pdo
yum install php-pdo
[2] pdo.so was loading after pdo_sqlsrv.so
/etc/opt/remi/php70/php.d/30-pdo_sqlsrv.ini
; mssql extension=sqlsrv.so extension=pdo_sqlsrv.so