如何从Ubuntu 18.04用PHP连接到MSSQL服务器?

uyto3xhc  于 2022-12-25  发布在  PHP
关注(0)|答案(3)|浏览(242)

我想从Ubuntu 18.04建立一个MSSQL连接。设置这个连接相当困难,但我知道它到目前为止工作正常,我可以使用sql_srv类或pdo类。但当我想连接时,连接失败,并显示错误

Array ( [0] => Array ( [0] => 01000 [SQLSTATE] => 01000 [1] => 0 [code] => 0 [2] => [unixODBC][Driver Manager]Can't open lib 'ODBC Driver 13 for SQL Server' : file not found [message] => [unixODBC][Driver Manager]Can't open lib 'ODBC Driver 13 for SQL Server' : file not found ) ).

如何解决这个问题?这个错误意味着什么?我已经安装了ODBC 17。

odbcinst -j
unixODBC 2.3.7
DRIVERS............: /etc/odbcinst.ini
SYSTEM DATA SOURCES: /etc/odbc.ini
FILE DATA SOURCES..: /etc/ODBCDataSources
USER DATA SOURCES..: /root/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8

然后nano/etc/odbcinst.ini显示:
[ODBC用于SQL Server的驱动程序17]说明=用于SQL Server的Microsoft ODBC驱动程序17驱动程序=/opt/microsoft/msodbcsql 17/lib 64/libmsodbcsql-17.5.so.1.1使用计数=1
我应该怎么做才能从ubuntu 18.04连接到MSSQL Server 2014?

rdrgkggo

rdrgkggo1#

解决:通过这三个页面,我可以安装正确的驱动程序版本:
https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver15#ubuntu17
Pecl install sqlsrv returns No releases available for package
http://pecl.php.net/package/sqlsrv/5.8.0下载驱动程序

sudo bash -c "echo extension=sqlsrv.so > /etc/php/7.2/mods-available/sqlsrv.ini"
sudo ln -s /etc/php/7.2/mods-available/sqlsrv.ini /etc/php/7.2/apache2/conf.d/sqlsrv.ini
sudo ln -s /etc/php/7.2/mods-available/sqlsrv.ini /etc/php/7.2/cli/conf.d/sqlsrv.ini
sudo bash -c "echo extension=pdo_sqlsrv.so > /etc/php/7.2/mods-available/pdo_sqlsrv.ini"
sudo ln -s /etc/php/7.2/mods-available/pdo_sqlsrv.ini /etc/php/7.2/apache2/conf.d/pdo_sqlsrv.ini
sudo ln -s /etc/php/7.2/mods-available/pdo_sqlsrv.ini /etc/php/7.2/cli/conf.d/pdo_sqlsrv.ini

sudo systemctl restart apache2
fwzugrvs

fwzugrvs2#

1.安装PECL
1.按照https://learn.microsoft.com/en-us/sql/connect/php/installation-tutorial-linux-mac?view=sql-server-ver15上提到的步骤操作
1.重新启动Apache
1.使用下面的PHP脚本测试连接

//format: serverName\instanceName, portNumber (default is 1433)
$serverName = "localhost";

$connectionInfo = array("Database" => "dbName", "UID" => "myUserName", "PWD" => "myPassword");
$conn = sqlsrv_connect($serverName, $connectionInfo);

if ($conn) {
    echo "Got a connection!<br />";
} else {
    echo "Connection could not be established.<br />";
    die(print_r(sqlsrv_errors(), true));
}
balp4ylt

balp4ylt3#

使用Microsoft Drivers for PHP for Microsoft SQL Server。您将在那里找到详细的安装说明。

相关问题