3

I'm running into a problem connecting to a MSSQL database from PHP.

We've got 2 applications on the same linux-server.

  • App A connects to a "simple" MSSQL server+database, and has no problem.
  • App B connects to a MSSQL server + INSTANCE + database, and fails to connect.

The fact that the database is within an "instance" of the MSSQL server seems to be the only difference.

The documentation appears not to specify how to connect to a database within an instance.

Example (pseudo) code:

<?php
$host="10.0.0.12";
$instance="specific"
$database="my-database"
$username="username";
$password="password";

$pdo = new PDO("dblib:host=${host}\\${instance};database=${database}", $username, $password);

This fails to connect, most of the time with a very uninformative message like:

SQLSTATE[HY000] Unable to connect: Adaptive Server is unavailable or does not exist (severity 9)

I've tried numerous different DSN's to see what might work, but none seem to work.

What DSN might I use to get this working?

Alternative ways to connect from PHP to this database are also welcome as suggestions.

1 Answer 1

4

Every MSSQL server instance runs on its own port number. You can configure the MSSQL Server instance to use a fixed port number instead of a dynamic one (which is the default).

Once you've configure a fixed port, you can simply connect to that port number using PDO. There is no further need to reference the instance in the dsn or any other setting.

Sign up to request clarification or add additional context in comments.

1 Comment

I have been trying to figure out how to connect to MSSQL instance for ages. Using the port number instead, and avoiding the instance name completely, has saved me. I know this is an old post, so I hope other people that have this issue come here first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.