简体   繁体   English

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

[英]DSN-less ODBC connections to SQL Server using PHP on RHEL

Is it possible to connect to Microsoft SQL Server using PHP and a DSN-less connection on RHEL? 是否可以使用PHP和RHEL上的无DSN连接连接到Microsoft SQL Server?

Content of /etc/odbcinst.ini /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 Code 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() );

Error: 错误:

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

Double checked my connection string here: http://www.connectionstrings.com/sql-server/ 在这里仔细检查了我的连接字符串: http : //www.connectionstrings.com/sql-server/

PHP's odbc_connect can accept a DSN-less connection strings: http://php.net/manual/en/function.odbc-connect.php PHP的odbc_connect可以接受无DSN的连接字符串: http : //php.net/manual/en/function.odbc-connect.php

The database source name for the connection. 连接的数据库源名称。 Alternatively, a DSN-less connection string can be used. 或者,可以使用无DSN的连接字符串。

PHP Info confirms that ODBC is loaded. 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

When doing an strace, it shows that /etc/odbc.ini and /home/me/odbc.ini gets accessed during the rendering of the page. 当执行strace时,它表明/etc/odbc.ini/home/me/odbc.ini在页面呈现期间被访问。 I did launch the page through the CLI to get these: 我确实通过CLI启动了页面以获取这些信息:

open("/etc/odbcinst.ini", O_RDONLY)     = 3
open("/home/me/.odbcinst.ini", O_RDONLY) = -1 ENOENT (No such file or directory)

The unfortunate fact is that all of this works if I use a DSN. 不幸的事实是,如果我使用DSN,所有这些都可以使用。 I really need for DSN-less to work however. 我真的需要少DSN才能工作。

Yes it is possible to use DSNless connections to MS SQL Server from PHP. 是的,可以使用从PHP到MS SQL Server的DSNless连接。

Change "Driver" to DRIVER in your connection string. 在连接字符串中将“ Driver”更改为DRIVER。

Run odbcinst -j to check where your system drivers file is located then ensure it has the driver in it. 运行odbcinst -j来检查系统驱动程序文件的位置,然后确保其中包含驱动程序。

If I repeat what you did in Perl I get: 如果我重复您在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.

Notice the first one fails like you get and the second finds the driver but I did not specify a servername so it basically demonstrates you need DRIVER. 请注意,第一个失败,就像您得到的,第二个找到了驱动程序,但是我没有指定服务器名,因此它基本上表明您需要DRIVER。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM