尝试使用 unixODBC/FreeTDS 连接到 PHP 中的 MS SQL Server 时出现 iODBC 错误

发布于 2024-09-09 08:54:09 字数 1114 浏览 0 评论 0原文

我正在尝试从 Mac 上的 PHP 连接到远程 MS SQL Server 数据库(最终在 Ubuntu 服务器上(使用 FreeTDS 和 unixODBC,但即使我似乎已正确设置所有内容,我仍收到 iODBC 错误,并且我'我不知道如何绕过它们,

我正在使用 MacPorts,所以我的配置是:

/opt/local/etc/freetds.conf::

[bti_db]
host = 123.45.67.89 (IP address changed to protect the innocent)
port = 14333
tds version = 8.0

/opt/local/etc/odbcinst.ini:

[FreeTDS]
Description = TDS Driver (Sybase/MSSQL)
Driver = /opt/local/lib/libtdsodbc.so
Setup = /opt/local/lib/libtdsS.so
FileUsage = 1

/opt/local/etc/ odbc.ini:

[bti_dsn]
Driver = FreeTDS
Description = My Database
Trace = no
Servername = bti_db
Database = btidata

但是,每当我尝试使用“bti_dsn”连接 odbc_connect() 时,

$conn = odbc_connect('bti_dsn;, $user, $pw);

都会收到此错误:

警告:odbc_connect() [function.odbc-connect]:SQL 错误:[iODBC][驱动程序管理器]数据源未找到名称且未指定默认驱动程序。无法加载驱动程序,SQLConnect 中的 SQL 状态 IM002 在

我的 phpinfo() 的 ODBC 部分中,我看到 ODBC 库定义为 iodbc,并且 PHP 是使用“--with-iodbc=/”编译的。 usr',所以我猜配置是我的问题。我怎样才能解决这个问题,以便它使用我设置的 unixODBC/FreeTDS

I am trying to connect to a remote MS SQL Server db from PHP on Mac (eventually on an Ubuntu server( with FreeTDS and unixODBC, but even though I seem to have everything set up properly, I'm getting iODBC errors, and I'm not sure how to get around them.

I'm using MacPorts, so my config is:

/opt/local/etc/freetds.conf::

[bti_db]
host = 123.45.67.89 (IP address changed to protect the innocent)
port = 14333
tds version = 8.0

/opt/local/etc/odbcinst.ini:

[FreeTDS]
Description = TDS Driver (Sybase/MSSQL)
Driver = /opt/local/lib/libtdsodbc.so
Setup = /opt/local/lib/libtdsS.so
FileUsage = 1

/opt/local/etc/odbc.ini:

[bti_dsn]
Driver = FreeTDS
Description = My Database
Trace = no
Servername = bti_db
Database = btidata

However, whenever I try to connect with odbc_connect() using the 'bti_dsn'

$conn = odbc_connect('bti_dsn;, $user, $pw);

I get this error:

Warning: odbc_connect() [function.odbc-connect]: SQL error: [iODBC][Driver Manager]Data source name not found and no default driver specified. Driver could not be loaded, SQL state IM002 in SQLConnect

In the ODBC section my phpinfo(), I see ODBC Library defined as iodbc, and PHP is compiled with '--with-iodbc=/usr', so I'm guessing that config is my problem. How can I get around this so that it uses unixODBC/FreeTDS I have set up?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

勿忘心安 2024-09-16 08:54:10

iODBC 默认情况下作为 Mac OS X 的一部分安装;自 Jaguar (10.2.x) 以来一直如此。 Mac 上不需要 UnixODBC,如果您不是认真的专家,它可能会导致很多错误。有一个关于在 Mac OS X 上使用 PHP 和 iODBC 的具体指南。为了获得最佳结果,您可能还需要升级到最新的 适用于 Mac OS 的 iODBC X

/opt/local/etc 不应通过 .profile 或其他方式添加到您的 $PATH 中。

PHP 肯定会先于 UnixODBC 找到 iODBC,但这应该不是问题; UnixODBC 和 iODBC 通常(并且应该是完全)API 等效的 ODBC 驱动程序管理器。如果您确实关心该部分,则可以更改 $DYLD_LIBRARY_PATH(Linux 的 $LD_LIBRARY_PATH 的 Mac OS X 版本)——但如果 PHP 与 iODBC 链接框架,相对于 dylib,这不会有任何区别。

(请注意,$DYLD_LIBRARY_PATH 还必须包含 /opt/local/lib,否则 FreeTDS 驱动程序将无法加载。)

对于您报告的特定错误 - PHP 需要具有如果您不使用 Mac 的默认 ODBC 配置文件,请设置几个环境变量(系统级别位于 /Library/ODBC/odbc[inst].ini 中;用户级别位于 ~/Library/ODBC/odbc[inst].ini ...如果存在 ~/.odbdc[inst].ini 文件,则应将它们混合到 ~/Library/ODBC/ 文件并替换为相同的符号链接)。

如果您不想使用 iODBC,或者不想使用这些默认文件,则必须将 $ODBCINI 设置为目标 odbc.ini 文件。已定义您的 DSN 和 $ODBCINSTINI 来定位注册您要使用的驱动程序的 odbcinst.ini 文件。

假设您想要执行上述所有操作,应将此类行添加到您的 *.php 文件中(最好通过 requireinclude语句以最大程度地减少将来的编辑)——

putenv("DYLD_LIBRARY_PATH=/path/to/odbcsdk/lib;$DYLD_LIBRARY_PATH");
putenv("ODBCINSTINI=/path/to/odbcinst.ini");
putenv("ODBCINI=/path/to/odbc.ini");

我无法准确了解 DYLD_LIBRARY_PATH 设置,因为您没有指定 UnixODBC 库的位置。但是,如果您同意 iODBC 作为驱动程序管理器,并且只想加载 FreeTDS 库,那么以下内容应该可以工作 -

putenv("DYLD_LIBRARY_PATH=/opt/local/lib;$DYLD_LIBRARY_PATH");
putenv("ODBCINSTINI=/opt/local/etc/odbcinst.ini");
putenv("ODBCINI=/opt/local/etc/odbc.ini");

我希望这会有所帮助。

PS 在您的 DSN 定义中,这一行 --

Driver = FreeTDS

-- 应该重写。人类友好的驱动程序名称应包含在大括号中 ({FreeTDS}),或者驱动程序库的完整路径 (/opt/local/lib/libtdsodbc.so) 应该是值。

Driver = {FreeTDS}
Driver = /opt/local/lib/libtdsodbc.so

我假设您的 odbcinst.ini 中也有类似以下索引条目的内容 --

[ODBC Drivers]
FreeTDS = Installed

-- 以及 odbc.ini 中类似以下索引条目的内容 - -

[ODBC Data Sources]
bti_dsn = FreeTDS

...但现在我注意到你的 $conn 行可能只需要修正。查看 odbc_connect 的参数。

$conn = odbc_connect('bti_dsn;, $user, $pw);

那可能看起来更像是——

$conn = odbc_connect("bti_dsn", "$user", "$pw");

iODBC is installed by default as part of Mac OS X; has been since Jaguar (10.2.x). There's no need for UnixODBC on Macs, and it can lead to lots of errors if you're not a serious expert. There is a specific guide to using PHP with iODBC on Mac OS X. For best results, you may also want to upgrade to the latest iODBC for Mac OS X.

/opt/local/etc should not be added to your $PATH, through .profile or otherwise.

PHP is definitely finding iODBC before UnixODBC, but this should not be a problem; UnixODBC and iODBC are generally (and are meant to be fully) API-equivalent ODBC driver managers. If you're really concerned about that part, you can change the $DYLD_LIBRARY_PATH (Mac OS X's version of Linux's $LD_LIBRARY_PATH) -- but if PHP was linked against the iODBC Frameworks, as opposed to the dylibs, this won't make any difference.

(Note that $DYLD_LIBRARY_PATH also must include /opt/local/lib or your FreeTDS driver won't load.)

For the specific error your report -- PHP needs to have a couple of environment variables set, if you're not using the Mac's default ODBC configuration files (System level are in /Library/ODBC/odbc[inst].ini; User level are in ~/Library/ODBC/odbc[inst].ini ... if there are ~/.odbdc[inst].ini files present, they should be blended into the ~/Library/ODBC/ files and replaced by symlinks to the same).

If you don't want to use iODBC, or don't want to use those default files, you have to set $ODBCINI to target the odbc.ini file where you've defined your DSN, and $ODBCINSTINI to target the odbcinst.ini file which registers the driver you want to use.

Assuming you want to do all of the above, lines like these should be added to your *.php files (optimally via a require or include statement to minimize future editing) --

putenv("DYLD_LIBRARY_PATH=/path/to/odbcsdk/lib;$DYLD_LIBRARY_PATH");
putenv("ODBCINSTINI=/path/to/odbcinst.ini");
putenv("ODBCINI=/path/to/odbc.ini");

I can't be exact about the DYLD_LIBRARY_PATH setting, because you didn't specify where your UnixODBC libraries are. However, if you are OK with iODBC being the driver manager, and just want your FreeTDS libraries to load, the following should work --

putenv("DYLD_LIBRARY_PATH=/opt/local/lib;$DYLD_LIBRARY_PATH");
putenv("ODBCINSTINI=/opt/local/etc/odbcinst.ini");
putenv("ODBCINI=/opt/local/etc/odbc.ini");

I hope this helps.

P.S. In your DSN definition, this line --

Driver = FreeTDS

-- should be rewritten. Either the human-friendly driver name should be wrapped in braces ({FreeTDS}), or the full-path to the driver library (/opt/local/lib/libtdsodbc.so) should be the value instead.

Driver = {FreeTDS}
Driver = /opt/local/lib/libtdsodbc.so

I'm presuming that you also have something like the following index entry in your odbcinst.ini --

[ODBC Drivers]
FreeTDS = Installed

-- and something like the following index entry in your odbc.ini --

[ODBC Data Sources]
bti_dsn = FreeTDS

...but now I notice that your $conn line may just need correction. Look at the arguments to odbc_connect.

$conn = odbc_connect('bti_dsn;, $user, $pw);

That should probably look more like --

$conn = odbc_connect("bti_dsn", "$user", "$pw");
你与清晨阳光 2024-09-16 08:54:10

看起来它没有在您的 odbc.ini 文件中查找。也许它正在寻找 /etc/odbc.ini/etc/odbcinst.ini

It looks like it's not looking in your odbc.ini file. Maybe it's looking for /etc/odbc.ini and /etc/odbcinst.ini?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文