一、首先是windows安装扩展
7.0.x的扩展下载地址:
Microsoft Drivers for PHP for SQL Server https://www.microsoft.com/en-us/download/details.aspx?id=20098
ODBC Driver:
Microsoft® ODBC Driver 11 for SQL Server® - Windows (支持Sql Server® 2005) https://www.microsoft.com/zh-CN/download/details.aspx?id=36434
Microsoft® ODBC Driver 13 for SQL Server® - Windows + Linux (支持最新的SQL Server® 2016) https://www.microsoft.com/zh-CN/download/details.aspx?id=50420
安装配置:
下载SQL Server的PHP扩展(Microsoft Drivers for PHP for SQL Server),连接里有多个文件,只需要下载 SQLSRV40.EXE 即可。
解压后根据自己PHP的版本选择32位或64位的扩展,注意区分(Thread safe?)nts和ts。
复制扩展需要的文件到PHP扩展目录。(例如我这里使用的是:php_pdo_sqlsrv_7_ts_x64.dll 和 php_sqlsrv_7_ts_x64.dll)
修改php.ini添加以下两行来启用扩展:
extension=php_sqlsrv_7_ts_x64.dll extension=php_pdo_sqlsrv_7_ts_x64.dll
安装ODBC Driver,根据自己的需要选择上面的地址下载并安装。
现在可以使用phpinfo()来查看是否成功加载了 pdo_sqlsrv 模块。
二、Centos安装扩展
前提貌似文档说一定要PHP7以后的版本,以前很多都是第三方FreeTDS,再怎么搞也没有微软官方的稳定吧!
1、加入微软的源
curl https://packages.microsoft.com/config/rhel/7/prod.repo > /etc/yum.repos.d/mssqlrelease.repo
2、防止冲突先卸载原有版本(可选)
yum remove unixODBC
3、安装驱动(三个都要装上,缺一不可)
yum install msodbcsql mssql-tools unixODBC-devel
4、下载pdo_sqlsrv扩展包
http://pecl.php.net/package/pdo_sqlsrv
5、以4.3.8为例
tar -zxvf pdo_sqlsrv-4.3.8.tgz
6、进入解压目录
cd pdo_sqlsrv-4.0.8
7、执行PHP的一个扩展命令
/usr/local/php/bin/phpize
8、编译
./configure --with-php-config=/usr/local/php/bin/php-config
9、安装
make && make install
10、修改/usr/local/php/etc/php.ini 添加extension
extension = "pdo_sqlsrv.so"
11、重启php-fpm和nginx,大功告成
下面我重点说我的踩坑记录:
windows安装完后,2种方式调用,我最开始用的sqlsrv_connect方式,能成功调用,具体的api参考这里。
https://docs.microsoft.com/en-us/sql/connect/php/sqlsrv-driver-api-reference
但是上面的安装方法,Centos服务器最终是只安装pdo_sqlsrv.so的。所以只能采用另外一种pdo方式调用,才是通用的。
https://github.com/Microsoft/msphpsql/edit/master/sample/pdo_sqlsrv_sample.php
<?php echo "\n"; $serverName = "tcp:yourserver.database.windows.net,1433"; $database = "yourdatabase"; $uid = "yourusername"; $pwd = "yourpassword"; //Establishes the connection $conn = new PDO( "sqlsrv:server=$serverName ; Database = $database", $uid, $pwd); //Select Query $tsql = "SELECT [CompanyName] FROM SalesLT.Customer"; //Executes the query $getProducts = $conn->query( $tsql ); //Error handling FormatErrors ($conn->errorInfo()); $productCount = 0; $ctr = 0; ?> <h1> First 10 results are : </h1> <?php while($row = $getProducts->fetch(PDO::FETCH_ASSOC)) { if($ctr>9) break; $ctr++; echo($row['CompanyName']); echo("<br/>"); $productCount++; } $getProducts = NULL; $tsql = "INSERT INTO SalesLT.Product (Name, ProductNumber, StandardCost, ListPrice, SellStartDate) OUTPUT INSERTED.* VALUES ('SQL New 1', 'SQL New 2', 0, 0, getdate())"; //Insert query $insertReview = $conn->query( $tsql ); FormatErrors ($conn->errorInfo()); ?> <h1> Product Key inserted is :</h1> <?php while($row = $insertReview->fetch(PDO::FETCH_ASSOC)) { echo($row['ProductID']."<br/>"); } $insertReview = NULL; //Delete Query //We are deleting the same record $tsql = "DELETE FROM [SalesLT].[Product] WHERE Name=?"; $param = "SQL New 1"; $deleteReview = $conn->prepare($tsql); $deleteReview->bindParam(1, $param); $deleteReview->execute(); FormatErrors ($deleteReview->errorInfo()); function FormatErrors( $error ) { /* Display error. */ echo "Error information: <br/>"; echo "SQLSTATE: ".$error[0]."<br/>"; echo "Code: ".$error[1]."<br/>"; echo "Message: ".$error[2]."<br/>"; } ?>