0

I am trying to connect to an external db which uses sqlserver but after trying many options couldnt find one that works at all.

    $connattempt1 = odbc_connect("Driver={SQL Server Native Client 10.0};Server=XXX.XXX.XXX.XX;1433;Database=IDD",'username','password');
    $connattempt2  = new PDO('sqlsrv:Server=XXX.XXX.XXX.XX,1433;Database=IDD','username','password');
    $connattempt3  = sqlsrv_connect('XXX.XXX.XXX.XX', [
            "Database"      =>      IDD, 
            "UID"           =>      'username',
            "PWD"           =>      'password',
            "CharacterSet"  =>      "UTF-8"
        ]);

I will be running this same code on several apache servers all of them on php7. Im looking for a way to connect without having to edit the php.ini on all servers or importing external libraries. Is there a standard way to correctly make this connection? Any clue, manual, tips, links are also welcome, thanks!

jarlh
  • 42,561
  • 8
  • 45
  • 63
Mbotet
  • 170
  • 2
  • 17
  • 1
    A small note - in the third attempt you need to surrond the database name with quotes: `"Database" => "IDD",` But, what error message do you receive from each of these three calls? – Zhorov Feb 05 '21 at 14:36
  • 2
    Why are you using SQL Server Native Client rather than ODBC Driver for SQL Server? SQL Server Native Client [has been deprecated](https://learn.microsoft.com/en-us/sql/relational-databases/native-client/sql-server-native-client?view=sql-server-ver15) for some time now. – Thom A Feb 05 '21 at 14:36
  • 1
    Also, unless I recall incorrectly, SQL Server 2012+ comes with version 11.0 of SQLNCLI; so you're using a version (of SQLNCLI) that doesn't support *any* of the currently supported versions of SQL Server (including those in extended support). I don't know what version of SQL Server you're trying to connect to, but if it's a supported one (2012+), the driver you are using is unlikely to support it correctly. – Thom A Feb 05 '21 at 14:40
  • @Zhorov im getting Call to undefined function odbc_connect() (and so on for all the others). After asking manager seems like only my apache problem. I checked we have those libraries correctly installed on other instances. I guess i have to install and then enable them. They dont come with basic xammp installation so ill let you all know if i managed to fix it after installing the packages – Mbotet Feb 08 '21 at 10:27
  • @Larnu just another option to try connect. As you can see its non working, but i was just showing im not a lazy ass who didnt try options. Thanks for the depreacated note though. I will take this in count – Mbotet Feb 08 '21 at 10:29
  • Well, I would personally start off by using a non-deprecated driver, and one that supports versions of SQL Server that are currently supported, @Mbotet. You likely want to be using the ODBC Driver 17 for SQL Server – Thom A Feb 08 '21 at 10:35
  • 1
    @Mbotet Yes, you need to install the drivers (as PHP extensions). And probably the PDO approach should be you first option. – Zhorov Feb 08 '21 at 11:23

1 Answers1

0

Some Xammp instalations dont come with common libraries, this was the case. After checking the configurations on some clients it seemed to work well and only my local machine lacked this libraries. This was the library i used: https://pecl.php.net/package/sqlsrv/5.9.0/windows This website is a really great resource to find any dll you might need, so i hope it will help.

Mbotet
  • 170
  • 2
  • 17