Reputation: 3478
Is it possible to connect to Microsoft SQL Server using PHP and a DSN-less connection on RHEL?
Content of /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
$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/
PHP's odbc_connect can accept a DSN-less connection strings: 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.
PHP Info confirms that ODBC is loaded.
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. I did launch the page through the CLI to get these:
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. I really need for DSN-less to work however.
Upvotes: 0
Views: 2914
Reputation: 5992
Yes it is possible to use DSNless connections to MS SQL Server from PHP.
Change "Driver" to DRIVER in your connection string.
Run odbcinst -j to check where your system drivers file is located then ensure it has the driver in it.
If I repeat what you did in Perl I get:
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.
Upvotes: 1