php pdo connection to mssql instance

依然范特西╮ 提交于 2019-12-24 13:58:16

问题


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:


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.



来源:https://stackoverflow.com/questions/27905596/php-pdo-connection-to-mssql-instance

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!