使用PHP连接到SQL Server时出错

6rqinv9w  于 2023-10-15  发布在  PHP
关注(0)|答案(1)|浏览(165)

我得到了这个错误:
“此扩展需要Microsoft ODBC Driver for SQL Server。访问以下URL以下载SQL Server for x64的ODBC驱动程序:https://go.microsoft.com/fwlink/?LinkId=163712
还有这个
“[Microsoft][ODBC Driver Manager]未找到数据源名称,且未指定默认驱动程序”
我已经安装了ODBC Driver就像在这个消息中说的那样。我在我的php.ini上添加了扩展并重新启动IIS,但仍然错误。

PHP version: 7.2.
Windows: Windows Server 2008 R2 x64
Sql Server: 2012 express

我的phpinfo PHPInfo
有人能帮我吗
使用的代码(简单的sqlsrv_connect)

$serverName = $this->host;
$connectionInfo["Database"] = $this->banco;
if ($this->user) $connectionInfo["UID"] = $this->user;
if ($this->pass) $connectionInfo["PWD"] = $this->pass;
if ($this->port) $serverName.=', '.$this->port;
$this->id_db = sqlsrv_connect( $serverName, $connectionInfo);

完整错误消息:

array(2) { 
    [0]=> array(6) { 
       [0]=> string(5) "IMSSP" 
       ["SQLSTATE"]=> string(5) "IMSSP" 
       [1]=> int(-49) 
       ["code"]=> int(-49) 
       [2]=> string(189) "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"]=> string(189) "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" 
    } 
    [1]=> array(6) { 
       [0]=> string(5) "IM002" 
       ["SQLSTATE"]=> string(5) "IM002" 
       [1]=> int(0) 
       ["code"]=> int(0) 
       [2]=> string(91) "[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified" 
       ["message"]=> string(91) "[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified" 
    } 
    }
tzcvj98z

tzcvj98z1#

Double check versions of drivers以确保它们可以一起工作。事实证明-这是非常重要的。
如果您使用ODBC Drivers的良好版本来使用PHP sqlsrv,请在此处检查。
对于PHP 7.2和sqlsrv-5.8.1,您需要使用Microsoft ODBC 17
如何安装它的更多信息在MS文档站点here
对于RHEL

# curl https://packages.microsoft.com/config/rhel/7/prod.repo | sudo tee /etc/yum.repos.d/mssql-release.repo

# sudo yum remove unixODBC-utf16 unixODBC-utf16-devel #to avoid conflicts
# sudo ACCEPT_EULA=Y yum install -y msodbcsql17

# sudo ACCEPT_EULA=Y yum install -y mssql-tools
# echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
# source ~/.bashrc

# sudo yum install -y unixODBC-devel

相关问题