Click to Rate and Give Feedback
MSDN
MSDN Library
SQL Server
SQL Server 2005
Community Content
In this section
Statistics Annotations (13)
Collapse All/Expand All Collapse All
SQL Server Driver for PHP Version 1.0 and Version 1.1
sqlsrv_connect

Creates a connection resource and opens a connection. By default, the connection is attempted using Windows Authentication.

sqlsrv_connect( string $serverName [, array $connectionInfo])

$serverName: A string specifying the name of the server to which a connection is being established. An instance name (for example, "myServer\instanceName") or port number (for example, "myServer, 1521") can be included as part of this string. For a complete description of the options available for this parameter, see the Server keyword in the ODBC Driver Connection String Keywords section of Using Connection String Keywords with SQL Native Client.

$connectionInfo [OPTIONAL]: An associative array that contains connection attributes (for example, array("Database" => "AdventureWorks")). The following table describes the supported keys for the array:

Key Value Description Default

APP

String

The application name used in tracing.

No value set.

CharacterSet

String

Specifies the character set used to send data to the server. This key is new in SQL Server Driver for PHP version 1.1.

For more information, see How to: Send and Retrieve UTF-8 Data Using Built-In UTF-8 Support.

SQLSRV_ENC_CHAR

ConnectionPooling

1 or true for connection pooling on.

0 or false for connection pooling off.

Specifies whether the connection is assigned from a connection pool (1 or true) or not (0 or false).

true (1)

Database

String

Specifies the name of the database in use for the connection being established1.

The default database for the login being used.

Encrypt

1 or true for encryption on.

0 or false for encryption off.

Specifies whether the communication with SQL Server is encrypted (1 or true) or unencrypted (0 or false)2.

false (0)

Failover_Partner

String

Specifies the server and instance of the database's mirror (if enabled and configured) to use when the primary server is unavailable.

No value set.

LoginTimeout

Integer

Specifies the number of seconds to wait before failing the connection attempt.

No timeout.

MultipleActiveResultSets

1 or true to use multiple active result sets.

0 or false to disable multiple active result sets.

Allows you to disable or explicitly enable support for multiple active Result sets (MARS). This key is new in SQL Server Driver for PHP version 1.1.

For more information, see How to: Disable Multiple Active Resultsets (MARS).

false (0)

PWD

String

Specifies the password associated with the User ID to be used when connecting with SQL Server Authentication3.

No value set.

QuotedId

1 or true to use SQL-92 rules.

0 or false to use legacy rules.

Specifies whether to use SQL-92 rules for quoted identifiers (1 or true) or to use legacy Transact-SQL rules (0 or false).

true (1)

ReturnDatesAsStrings

1 or true to return date and time types as strings.

0 or false to return date and time types as PHP DateTime types.

You can retrieve date and time types (datetime, date, time, datetime2, and datetimeoffset) as strings or as PHP types.

For more information, see How to: Retrieve Date and Time Type as Strings.

false

TraceFile

String

The path for the file used for trace data.

No value set.

TraceOn

1 or true to enable tracing.

0 or false to disable tracing.

Specifies whether ODBC tracing is enabled (1 or true) or disabled (0 or false) for the connection being established.

false (0)

TransactionIsolation

SQLSRV_TXN_READ_UNCOMMITTED,

SQLSRV_TXN_READ_COMMITTED,

SQLSRV_TXN_REPEATABLE_READ,

SQLSRV_TXN_SNAPSHOT,

SQLSRV_TXN_SERIALIZABLE

Specifies the transaction isolation level.

SQLSRV_TXN_READ_COMMITTED

TrustServerCertificate

1 or true to trust certificate.

0 or false to not trust certificate.

Specifies whether the client should trust (1 or true) or reject (0 or false) a self-signed server certificate.

false (0)

UID

String

Specifies the User ID to be used when connecting with SQL Server Authentication3.

No value set.

WSID

String

The name of the computer for tracing.

No value set.

1. All queries executed on the established connection will be made to the database that is specified by the Database attribute. However, data in other databases can be accessed by using a fully-qualified name if the user has the appropriate permissions. For example, if the master database is set with the Database connection attribute, it is still possible to execute a Transact-SQL query that accesses the AdventureWorks.HumanResources.Employee table by using the fully-qualified name.

2. Enabling Encryption can impact the performance of some applications due to the computational overhead required to encrypt data.

3. The UID and PWD attributes must both be set when connecting with SQL Server Authentication.

Many of the supported keys are ODBC connection string attributes. For information about ODBC connection strings, see Using Connection String Keywords with SQL Native Client.

A PHP connection resource. If a connection cannot be successfully created and opened, false is returned.

If values for the UID and PWD keys are not specified in the optional $connectionInfo parameter, the connection will be attempted using Windows Authentication. For more information about connecting to the server, see How to: Connect Using Windows Authentication and How to: Connect Using SQL Server Authentication.

The following example creates and opens a connection using Windows Authentication. The example assumes that SQL Server and the AdventureWorks database are installed on the local computer. All output is written to the console when the example is run from the command line.

<?php
/*
Connect to the local server using Windows Authentication and specify
the AdventureWorks database as the database in use. To connect using
SQL Server Authentication, set values for the "UID" and "PWD"
 attributes in the $connectionInfo parameter. For example:
$connectionInfo = array("UID" => $uid, "PWD" => $pwd)); 
*/
$serverName = "(local)";
$connectionInfo = array( "Database"=>"AdventureWorks");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn )
{
     echo "Connection established.\n";
}
else
{
     echo "Connection could not be established.\n";
     die( print_r( sqlsrv_errors(), true));
}

//-----------------------------------------------
// Perform operations with connection.
//-----------------------------------------------

/* Close the connection. */
sqlsrv_close( $conn);
?>
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker