PDO_SQLSRV DSN

(PECL pdo_sqlsrv >= 2.0.1)

PDO_SQLSRV DSNMS SQL Server および SQL Azure データベースに接続する

説明

PDO_SQLSRV データソース名 (DSN) は以下の要素で構成されます。

DSN 接頭辞

DSN 接頭辞は sqlsrv: です。

APP

トレースに使うアプリケーション名を指定します。

ConnectionPooling

接続をコネクションプールから確保する (1 あるいは true) のかしない (0 あるいは false) のかを指定します。

Database

データベースの名前を指定します。

Encrypt

SQL Server との通信を暗号化する (1 あるいは true) のかしない (0 あるいは false) のかを指定します。

Failover_Partner

サーバーと、データベースのインスタンスのミラー (有効になっていて、かつ使える場合) を指定して、プライマリーサーバーが使えないときに利用します。

LoginTimeout

接続の試行のタイムアウト秒数を指定します。

MultipleActiveResultSets

マルチアクティブ結果セット (MARS) のサポートを無効にするか、明示的に有効にします。

QuotedId

SQL-92 の規則に従った識別子のクォートを使う (1 あるいは true) か、レガシーな Transact-SQL のルールを使う (0 あるいは false) かを指定します。

Server

データベースサーバーの名前を指定します。

TraceFile

データのトレースに使うファイルのパスを指定します。

TraceOn

確立した接続用の ODBC のトレースを有効にする (1 あるいは true) か無効にする (0 あるいは false) かを指定します。

TransactionIsolation

トランザクション隔離レベルを指定します。このオプションに指定できる値は PDO::SQLSRV_TXN_READ_UNCOMMITTED、PDO::SQLSRV_TXN_READ_COMMITTED、 PDO::SQLSRV_TXN_REPEATABLE_READ、PDO::SQLSRV_TXN_SNAPSHOT および PDO::SQLSRV_TXN_SERIALIZABLE のいずれかです。

TrustServerCertificate

自己署名のサーバー証明書をクライアントに信頼させる (1 あるいは true) か させない (0 あるいは false) かを指定します。

WSID

トレース用のコンピューター名を指定します。

例1 PDO_SQLSRV DSN の例

以下の例は、MS SQL Server データベースに接続する方法を示します。

$c = new PDO("sqlsrv:Server=localhost;Database=testdb", "UserName", "Password");

以下の例は、ポートを指定して MS SQL Server データベースに接続する方法を示します。

$c = new PDO("sqlsrv:Server=localhost,1521;Database=testdb", "UserName", "Password");

以下の例は、サーバー ID 12345abcde の SQL Azure データベースに接続する方法を示します。 SQL Azure に PDO で接続するときは、ユーザー名が UserName@12345abcde (UserName@ServerId) となることに注意しましょう。

$c = new PDO("sqlsrv:Server=12345abcde.database.windows.net;Database=testdb", "UserName@12345abcde", "Password");

add a note add a note

User Contributed Notes 4 notes

up
3
Daniel Klein
6 years ago
This is the code that worked for me. I had to specify both the host name [localhost] and the server instance name [SQLEXPRESS], separating them with a double backslash before it would work.

<?php
$conn
= new PDO('sqlsrv:Server=localhost\\SQLEXPRESS;Database=MyDatabase', 'MyUsername', 'MyPassword');
?>
up
4
david at nospam-rm-this dot functionalchaos dot net
10 years ago
I found with MS-SQL Server 2008 on Windows Server 2008 R2 that I needed to use the MSSQL Server name. The servers hostname or IP address would not work ( with or without port, and/or SQL instance name, etc.) After trying several combinations, this is what I found worked in my configuration:

host-ip: 10.4.2.50
host-name: mssqlhost.mydomain.org
mssql-server-name: foo-sql
mssql-instance-name: MSSQLSERVER
database-name: mydb

<?php

$dbh
= new PDO("sqlsrv:Server=foo-sql,1433;Database=mydb", $user , $pass);

?>

The above worked with and without port (,1433). I also noticed adding the instance name (MSSQLSERVER) after the server name with a slash (\), as in "Server=foo-sql\MSSQLSERVER" caused a failure to connect.
up
1
support at spam eaccounts trap net
10 years ago
I suspect the problem with "Server=foo-sql\MSSQLSERVER" was you didn't escape the backslash, with a backslash.

If hostname worked, than IP would work as well given a hostname resolves to an IP unless your DNS was mapping to the wrong IP which would be a simple issue.

I suspect you are running your web services on the same host as mssql?  If so, confirm that your mssql server is set to bind to IP addresses (if you want access from the outside of that box).  If you are only able to connect via a name (as in a NetBIOS or Active Directory name), it's likely you are connecting to a "socket" or named pipe.
up
-46
alasdair at angryloner dot com dot au
9 years ago
Adding the backslash solved the problem for me.

Here's my functioning localdb connection string:

new PDO("sqlsrv:Server=(localdb)\\v11.0 ; Database = my_db ; AttachDBFilename = C:\Users\user\my_db.mdf", "", "");
To Top