首先说明,安装部分,我是转载的。
一、首先是windows安装扩展,转载自:https://www.cnblogs.com/huixch/p/7065033.html
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安装扩展,转载自:http://www.thinkphp.cn/topic/47058.html
前提貌似文档说一定要PHP7以后的版本,以前很多都是第三方FreeTDS,再怎么搞也没有微软官方的稳定吧!
1、加入微软的源
- curl https://packages.microsoft.com/config/rhel/7/prod.repo > /etc/yum.repos.d/mssqlrelease.repo
- yum remove unixODBC
- yum install msodbcsql mssql-tools unixODBC-devel
5、以4.3.8为例
- tar -zxvf pdo_sqlsrv-4.3.8.tgz
- cd pdo_sqlsrv-4.0.8
- /usr/local/php/bin/phpize
- ./configure --with-php-config=/usr/local/php/bin/php-config
- make && make install
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/>";
}
?>