2

I'm trying to connect to my MSSQL server through Laravel. But for some reason I get this error every time. I'm using PHP 7.3

1 PDOException::("SQLSTATE[01002] Adaptive Server connection failed (myserver.com:1433) (severity 9)")

I'm able to connect though, with this command:

TDSVER=7.3 tsql -H myserver.com -U 'user' -D 'database' -p 1433 -P 'password'

which leads me to believe it's an issue with how my PHP is configured. I'm using FreeTDS and here's my config:

#   $Id: freetds.conf,v 1.12 2007-12-25 06:02:36 jklowden Exp $
#
# This file is installed by FreeTDS if no file by the same 
# name is found in the installation directory.  
#
# For information about the layout of this file and its settings, 
# see the freetds.conf manpage "man freetds.conf".  

# Global settings are overridden by those in a database
# server specific section
[global]
        # TDS protocol version
;       tds version = 7.3

        tds version = 8.0

        client charset = UTF-8
        # Whether to write a TDSDUMP file for diagnostic purposes
        # (setting this to /tmp is insecure on a multi-user system)
;       dump file = /tmp/freetds.log
;       debug flags = 0xffff

        # Command and connection timeouts
;       timeout = 10
;       connect timeout = 10

        # If you get out-of-memory errors, it may mean that your client
        # is trying to allocate a huge buffer for a TEXT field.  
        # Try setting 'text size' to a more reasonable limit 
        text size = 20971520

        # If you experience TLS handshake errors and are using openssl,
        # try adjusting the cipher list (don't surround in double or single quotes)
        # openssl ciphers = HIGH:!SSLv2:!aNULL:-DH

# A typical Sybase server
[egServer50]
        host = symachine.domain.com
        port = 5000
        tds version = 7.3

# A typical Microsoft server
[egServer70]
        host = ntmachine.domain.com
        port = 1433
        tds version = 7.3

Here's my connection code:

$conn = new \PDO("dblib:host = myserver.com,1433; Database = mydb", "user", 'password');

here are some similar questions, but their solution didn't work for me:

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

Eliahu Aaron
  • 4,103
  • 5
  • 27
  • 37
Carter Bland
  • 33
  • 1
  • 5
  • For future people, here's what helped me. https://learn.microsoft.com/en-us/sql/connect/php/installation-tutorial-linux-mac?view=sql-server-2017#installing-the-drivers-on-ubuntu-1604-1804-and-1810 – Carter Bland Jul 16 '19 at 21:34

2 Answers2

1

You must fix your connection string. Change dlib to sqlsrv and host to Server. More details here

$conn = new \PDO("sqlsrv:Server=myserver.com,1433;Database=mydb", "user", 'password');

Caconde
  • 4,177
  • 7
  • 35
  • 32
0

Use TDS version 7.3 in the freetds.conf and change the connection code to:

new PDO ("dblib:host=myserver.com:1433;dbname=mydb","user","password");

Note that the dblib is supported only on PHP 5.6 (which itself is no longer supported), so you better use the Microsoft's SQLSRV drivers.

slaakso
  • 8,331
  • 2
  • 16
  • 27