You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress中PHP远程连接SQL Server失败问题求助

WordPress环境下PHP远程连接SQL Server失败的解决办法

问题重现

在WordPress中尝试用PHP远程连接SQL Server,使用的代码如下:

$serverName = "IP"; 
$username = "username";   
$password = "password";  
$database = "database"; 

$connectionInfo = array( "Database"=> $database, "UID"=>$username, "PWD"=>$password);
$conn = sqlsrv_connect($serverName, $connectionInfo);
if( $conn ) {
     echo "Connection established.";
}else{
     echo "Connection could not be established.";
     die( print_r( sqlsrv_errors(), true));
}

执行后出现错误:

Connection could not be established.Array
(
    [0] => Array
        (
            [0] => IMSSP
            [SQLSTATE] => IMSSP
            [1] => -49
            [code] => -49
            [2] => This extension requires the Microsoft ODBC Driver for SQL Server. Access the following URL to download the ODBC Driver for SQL Server for x64: https://go.microsoft.com/fwlink/?LinkId=163712 
            [message] => This extension requires the Microsoft ODBC Driver for SQL Server. Access the following URL to download the ODBC Driver for SQL Server for x64: https://go.microsoft.com/fwlink/?LinkId=163712
        )

)

已在WordPress的PHP设置中启用sqlsrv和pdo_sqlsrv扩展:
已启用sqlsrv和pdo_sqlsrv的配置界面

问题根源

错误信息明确指出:sqlsrv扩展必须依赖Microsoft ODBC Driver for SQL Server才能正常工作。即便已经开启了PHP的sqlsrv扩展,若运行WordPress的服务器上未安装对应ODBC驱动,连接必然失败。

解决步骤

  • 确认服务器架构:根据错误提示优先选择x64版本驱动,若服务器为x86架构则下载对应版本。
  • 安装匹配的ODBC驱动:在WordPress所在服务器上安装与PHP版本、SQL Server版本兼容的Microsoft ODBC Driver for SQL Server。建议优先安装最新稳定版,确保和sqlsrv扩展版本适配(比如PHP 7.4适配ODBC 17/18,PHP 8.0+优先用ODBC 18)。
  • 重启服务:驱动安装完成后,重启Web服务器(如Apache、Nginx)或PHP-FPM服务,使驱动生效。
  • 重新测试连接:再次执行连接代码,验证是否成功建立连接。

额外排查点

  • 若安装驱动后仍报错,先核查驱动与PHP版本的兼容性,版本不匹配是常见诱因。
  • 远程SQL Server的默认端口1433是否开放?不过当前错误并非端口问题,优先解决ODBC驱动缺失问题即可。

内容的提问来源于stack exchange,提问作者Rita Melo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 01:10:32