在RHEL上使用PHP的无DSN的SQL Server连接

时间:2014-12-17 20:11:11

标签: php sql-server odbc

是否可以使用PHP和RHEL上的无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-less   可以使用连接字符串。

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工作。

1 个答案:

答案 0 :(得分:1)

是的,可以使用PHP的DSNless连接到MS SQL Server。

在连接字符串中将“Driver”更改为DRIVER。

运行odbcinst -j以检查系统驱动程序文件的位置,然后确保其中包含驱动程序。

如果我重复你在Perl中所做的事情,我会得到:

perl -MDBI -le 'my $x = DBI->connect("dbi:ODBC:Driver={Easysoft ODBC-SQL Server}");'

DBI connect('Driver={Easysoft ODBC-SQL Server}','',...) failed: [unixODBC][Driver Manager]Data source name not found, and no default driver specified (SQL-IM002) at -e line 1.
perl -MDBI -le 'my $x = DBI->connect("dbi:ODBC:DRIVER={Easysoft ODBC-SQL Server}");'

DBI connect('DRIVER={Easysoft ODBC-SQL Server}','',...) failed: [unixODBC][Easysoft][SQL Server Driver][SQL Server]General error: server name not specified (SQL-HY000) at -e line 1.

注意第一个失败就像你得到的那样,第二个找到了驱动程序,但我没有指定服务器名,所以它基本上证明你需要DRIVER。