使用Microsoft Drivers for PHP for SQL Server操作SQL Server 2012

需求:用php5.6.3操作SQL Serverphp5.3以后这个功能由微软提供的实现。

我的环境:WIN8.1,XAMPP(PHP5.6.3),SQL Server2012

 

step1:

首先去官网下载https://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx,根据自己PHP版本下载相应的内容:

(如何查看PHP版本:在地址栏输入127.0.0.1:8088/index.php,点击phpinfo(),即可查看PHP版本,具体如图:

 

根据自己PHP的版本,勾选相应的内容,并点击“Next”下载:

 

step2:

下载到本地电脑上,可直接右键解压:

 

 step3:

解压后可得到下图,这里面有很多文件,一般我们需要的只有两个,可以这样判断:

(1)       是54?55?还是56?

根据你的PHP版本,

若是5.4.X,就选带有54的;

若是5.5.X,就选带有54的;

若是5.6.X,就选带有56的。

如下图,我的是5.6.3,所以选择带有56的,可以选出来4个,分别是:

php_pdo_sqlsrv_56_ts.dll

php_pdo_sqlsrv_56_nts.dll

php_sqlsrv_56_ts.dll

php_sqlsrv_56_nts.dll

 

(2)       再排除,根据ts还是nts?

在phpinfo()页面里,找打“Thread Safety”(线程安全),后面是enabled,选ts;不是,选nts。

我的是enabled,选ts,最终还剩两个:

php_pdo_sqlsrv_56_ts.dll

php_sqlsrv_56_ts.dll

 

 

 step4:

将最终选出来的两个复制到:xampp\php\ext中

并在php.ini的最后加入:

[PHP_PDO_SQLSRV]

extension=php_pdo_sqlsrv_56_ts.dll

[PHP_SQLSRV]

extension=php_sqlsrv_56_ts.dll

当然了,上面extension=后是你刚才复制的两个文件的文件名!!!

 step5:

再重启Apache就行了。

 

 

运行后后出现了的问题,提示如下:

(1)This extension requires the Microsoft SQL Server 2012 Native Client. Access the following URL to download the Microsoft SQL Server 2012 Native Client ODBC driver for x86: http://go.microsoft.com/fwlink/?LinkId=163712

按照上面给出的地址,下载下来后安装就行。

 

(2)PHP查询SQL  Server数据库中的中文显示乱码,是编码问题,解决如下:

$serverName = 'CLO';

$connectionInfo = array( 'Database'=>'CLO','UID'=>'lf','PWD'=>'456',"CharacterSet"=>"UTF-8");

$conn = sqlsrv_connect( $serverName, $connectionInfo);

 

 

(3) [ODBC Driver 11 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [53].

Microsoft][ODBC Driver 11 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.

对于这个问题,查看网上资料,是命名管道的问题,这里我不太懂,摆弄很久后,如下图设置,问题解决。

 

posted @ 2015-03-30 11:06  ZillaCui  阅读(345)  评论(0)    收藏  举报