一、安装PDO插件
二、安装PDO_SQLSRV插件
wget http://pecl.php.net/get/pdo_sqlsrv-5.8.0.tgz
三、安装Microsoft ODBC Driver for SQL Server
Microsoft ODBC 17 - ubuntu:
if ! [[ "16.04 18.04 20.04 22.04" == *"$(lsb_release -rs)"* ]]; then echo "Ubuntu $(lsb_release -rs) is not currently supported."; exit; fi sudo su curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - curl https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/prod.list > /etc/apt/sources.list.d/mssql-release.list exit sudo apt-get update sudo ACCEPT_EULA=Y apt-get install -y msodbcsql17 # optional: for bcp and sqlcmd sudo ACCEPT_EULA=Y apt-get install -y mssql-tools echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc source ~/.bashrc # optional: for unixODBC development headers sudo apt-get install -y unixodbc-dev
Microsoft ODBC 17 - Linux:
sudo su #Download appropriate package for the OS version #Choose only ONE of the following, corresponding to your OS version #RHEL 7 and Oracle Linux 7 curl https://packages.microsoft.com/config/rhel/7/prod.repo > /etc/yum.repos.d/mssql-release.repo #RHEL 8 and Oracle Linux 8 curl https://packages.microsoft.com/config/rhel/8/prod.repo > /etc/yum.repos.d/mssql-release.repo #RHEL 9 curl https://packages.microsoft.com/config/rhel/9.0/prod.repo > /etc/yum.repos.d/mssql-release.repo exit sudo yum remove unixODBC-utf16 unixODBC-utf16-devel #to avoid conflicts sudo ACCEPT_EULA=Y yum install -y msodbcsql17 # optional: for bcp and sqlcmd sudo ACCEPT_EULA=Y yum install -y mssql-tools echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc source ~/.bashrc # optional: for unixODBC development headers sudo yum install -y unixODBC-devel
官网链接文档:https://learn.microsoft.com/zh-cn/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver16&tabs=alpine18-install%2Credhat17-install%2Cubuntu16-install%2Credhat7-13-install%2Crhel7-offline
四、PHP连接代码
// 定义连接信息 $serverName = "192.168.0.1"; $databaseName = "master"; $username = "sa"; $password = "123"; $options = array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::SQLSRV_ATTR_DIRECT_QUERY => false, PDO::SQLSRV_ATTR_CLIENT_BUFFER_MAX_KB_SIZE => 1024, ); // 连接到数据库 $conn = new PDO("sqlsrv:Server=$serverName;Database=$databaseName", $username, $password);
来源:原创
发布时间:2023-08-02 11:25:37