11

I'm connecting to external MSSQL database for exports from PHP55/osx and I have wierd issue.

code:

new \PDO("dblib:host={$hostname};dbname={$dbname}", $user, $pass);

throws:

SQLSTATE[01002] Adaptive Server connection failed (severity 9)

but connection from CLI works correcty

tsql -S hostname -U user -P pass -L dbname:

locale is "cs_CZ.UTF-8"
locale charset is "UTF-8"
using default charset "UTF-8"
1> 

freetds.conf:

[hostname]
    host = ipaddress
    port = 1433
    tds version = 8.0

tsql -C:

                        Version: freetds v0.91
         freetds.conf directory: /usr/local/Cellar/freetds/0.91/etc
 MS db-lib source compatibility: no
    Sybase binary compatibility: no
                  Thread safety: yes
                  iconv library: yes
                    TDS version: 7.1
                          iODBC: no
                       unixodbc: no
          SSPI "trusted" logins: no
                       Kerberos: no

Any ideas? I have tried alomost anything, connect to hostname, ip, with and without port, to instance name, another user, TDS versions 7.0,7.1,7.2,8.0, reinstall php and freetds

Jakub Riedl
  • 1,066
  • 2
  • 10
  • 27
  • 2
    Have you tried not including the `dbname` in the connection and the later on using `mssql_select_db`? – simeg Jul 26 '14 at 13:41
  • Can you try another driver like ODBC ? I already show weird results with dblib. – Kevin Labécot Jul 29 '14 at 06:16
  • Like said above, it may also be related to your dbname attribute. – Kevin Labécot Jul 29 '14 at 06:28
  • I have tried ODBC but I cannot connect with it at all. Probably I set something wrong. I really prefer dblib because of it is supported by my abstraction (Laravel Eloquent ORM). I tried it even without dbname but it didn't help – Jakub Riedl Jul 29 '14 at 12:55
  • 1. Try to add this line in freetds.conf: client charset = UTF-8 ##needed on MAC OS X – TonyMkenu Jul 30 '14 at 13:59
  • 2. try to chnage the TDS version in the FreeTDS.conf (under [global]) to 7.0 – TonyMkenu Jul 30 '14 at 14:00
  • [This guy](http://stackoverflow.com/questions/22253649/cant-establish-connection-between-php-and-sql-server-unix) says he changed the [global] version to 7.0 and it worked for him. – Zombiesplat Jul 30 '14 at 15:08
  • i cannot reproduce the bugs since i don't use mssql. But I did search for it for a while and i found this thread: http://php.net/manual/zh/ref.pdo-dblib.php#94175 i hope it is a good start for you. – Ivan Jul 31 '14 at 04:11
  • 2
    I have exactly the same problem. And tried EVERYTHING, without success. The only difference is that when I do `tsql -C` I get `TDS version: 5.0`. – jplozano Apr 09 '15 at 13:45

2 Answers2

7

Check these:

  1. locate freetds.conf on your disk. It is possible it exists in several places and tsql uses one while PHP used another one. Best is to symlink them into one common file and test on that. Note that a common place for that file is ~/.freetds.conf beside /etc/ or /usr/local/etc/

  2. there should be a [global] section on your freetds.conf file. Put there these lines :

    tds version = 8.0

    text size = 20971520

    client charset = UTF-8

Of course, I expect that you already checked which driver is loaded by PHP with phpinfo(): mssql, sqlsrv or dblib

Jorj
  • 2,495
  • 1
  • 19
  • 12
2

My solution:

Get FreeTds

  • brew install freetds
  • copy everything in the lib/ folder of your freetds installation into the lib/ folder of your PHP installation directory, replacing the old versions of: libct.4.dylib, libct.a, libct.dylib, libsybdb.5.dylib, libsybdb.a, libsybdb.dylib

Compile and install the dblib extension

  • download the php source for whatever php version you have installed
  • navigate to the ext/pdo_dblib subfolder of the source
  • run phpize, making sure it is the correct binary for your installation
  • run the configure command with e.g. parameters: ./configure --with-php-config=/usr/local/php5/bin/php-config --with-pdo-dblib=/usr/local/Cellar/freetds/0.95.19/
  • run make
  • copy modules/pdo_dblib.so into your PHP installation directory, replacing the old version
Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
Joaquim d'Souza
  • 1,416
  • 2
  • 14
  • 25