【发布时间】:2014-12-17 20:11:10
【问题描述】:
是否可以在 RHEL 上使用 PHP 和无 DSN 连接连接到 Microsoft SQL Server?
/etc/odbcinst.ini的内容
[SQL Server Native Client 11.0]
Description=Microsoft SQL Server ODBC Driver V1.0 for Linux
Driver=/opt/microsoft/sqlncli/lib64/libsqlncli-11.0.so.1790.0
Threading=1
UsageCount=1
PHP 代码
$dsn = "Driver={SQL Server Native Client 11.0};Server=server_ip_here;Database=database_here;User Id=username_here;PWD=password_here";
$con = odbc_connect($dsn,$duser,$dpas) or die(odbc_errormsg() );
错误:
Warning: odbc_connect(): SQL error: [unixODBC][Driver Manager]Data source name not found, and no default driver specified, SQL state IM002 in SQLConnect in /path/to/file/index.php on line 152 [unixODBC][Driver Manager]Data source name not found, and no default driver specified
在这里仔细检查了我的连接字符串: http://www.connectionstrings.com/sql-server/
PHP 的 odbc_connect 可以接受无 DSN 的连接字符串: http://php.net/manual/en/function.odbc-connect.php
连接的数据库源名称。或者,无 DSN 可以使用连接字符串。
PHP Info 确认 ODBC 已加载。
ODBC Support enabled
Active Persistent Links 0
Active Links 0
ODBC library unixODBC
ODBC_INCLUDE -I/usr/include
ODBC_LFLAGS -L/usr/lib64
ODBC_LIBS -lodbc
Directive Local Value Master Value
odbc.allow_persistent On On
odbc.check_persistent On On
odbc.default_cursortype Static cursor Static cursor
odbc.default_db no value no value
odbc.default_pw no value no value
odbc.default_user no value no value
odbc.defaultbinmode return as is return as is
odbc.defaultlrl return up to 4096 bytes return up to 4096 bytes
odbc.max_links Unlimited Unlimited
odbc.max_persistent Unlimited Unlimited
在执行 strace 时,它显示在页面呈现期间访问了 /etc/odbc.ini 和 /home/me/odbc.ini。我确实通过 CLI 启动了该页面以获取这些:
open("/etc/odbcinst.ini", O_RDONLY) = 3
open("/home/me/.odbcinst.ini", O_RDONLY) = -1 ENOENT (No such file or directory)
不幸的是,如果我使用 DSN,所有这些都可以正常工作。但是,我真的需要 DSN-less 才能工作。
【问题讨论】:
标签: php sql-server odbc