<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Opens a connection to a Microsoft SQL Server database</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.sqlsrv-configure.html">sqlsrv_configure</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.sqlsrv-errors.html">sqlsrv_errors</a></div> <div class="up"><a href="ref.sqlsrv.html">SQLSRV Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.sqlsrv-connect" class="refentry"> <div class="refnamediv"> <h1 class="refname">sqlsrv_connect</h1> <p class="verinfo">(No version information available, might only be in SVN)</p><p class="refpurpose"><span class="refname">sqlsrv_connect</span> — <span class="dc-title">Opens a connection to a Microsoft SQL Server database</span></p> </div> <div class="refsect1 description" id="refsect1-function.sqlsrv-connect-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">resource</span> <span class="methodname"><strong>sqlsrv_connect</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$serverName</code></span> [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$connectionInfo</code></span> ] )</div> <p class="para rdfs-comment"> Opens a connection to a Microsoft SQL Server database. By default, the connection is attempted using Windows Authentication. To connect using SQL Server Authentication, include "UID" and "PWD" in the connection options array. </p> </div> <div class="refsect1 parameters" id="refsect1-function.sqlsrv-connect-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">serverName</code></em></span> <dd> <p class="para"> The name of the server to which a connection is established. To connect to a specific instance, follow the server name with a forward slash and the instance name (e.g. serverName\sqlexpress). </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">connectionInfo</code></em></span> <dd> <p class="para"> An associative array that specifies options for connecting to the server. If values for the UID and PWD keys are not specified, the connection will be attempted using Windows Authentication. For a complete list of supported keys, see <a href="http://msdn.microsoft.com/en-us/library/ff628167.aspx" class="link external">» SQLSRV Connection Options</a>. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.sqlsrv-connect-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> A connection resource. If a connection cannot be successfully opened, <strong><code>FALSE</code></strong> is returned. </p> </div> <div class="refsect1 examples" id="refsect1-function.sqlsrv-connect-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-2172"> <p><strong>Example #1 Connect using Windows Authentication.</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$serverName </span><span style="color: #007700">= </span><span style="color: #DD0000">"serverName\sqlexpress"</span><span style="color: #007700">; </span><span style="color: #FF8000">//serverName\instanceName<br /><br />// Since UID and PWD are not specified in the $connectionInfo array,<br />// The connection will be attempted using Windows Authentication.<br /></span><span style="color: #0000BB">$connectionInfo </span><span style="color: #007700">= array( </span><span style="color: #DD0000">"Database"</span><span style="color: #007700">=></span><span style="color: #DD0000">"dbName"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$conn </span><span style="color: #007700">= </span><span style="color: #0000BB">sqlsrv_connect</span><span style="color: #007700">( </span><span style="color: #0000BB">$serverName</span><span style="color: #007700">, </span><span style="color: #0000BB">$connectionInfo</span><span style="color: #007700">);<br /><br />if( </span><span style="color: #0000BB">$conn </span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"Connection established.<br />"</span><span style="color: #007700">;<br />}else{<br /> echo </span><span style="color: #DD0000">"Connection could not be established.<br />"</span><span style="color: #007700">;<br /> die( </span><span style="color: #0000BB">print_r</span><span style="color: #007700">( </span><span style="color: #0000BB">sqlsrv_errors</span><span style="color: #007700">(), </span><span style="color: #0000BB">true</span><span style="color: #007700">));<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> <div class="example" id="example-2173"> <p><strong>Example #2 Connect by specifying a user name and password.</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$serverName </span><span style="color: #007700">= </span><span style="color: #DD0000">"serverName\sqlexpress"</span><span style="color: #007700">; </span><span style="color: #FF8000">//serverName\instanceName<br /></span><span style="color: #0000BB">$connectionInfo </span><span style="color: #007700">= array( </span><span style="color: #DD0000">"Database"</span><span style="color: #007700">=></span><span style="color: #DD0000">"dbName"</span><span style="color: #007700">, </span><span style="color: #DD0000">"UID"</span><span style="color: #007700">=></span><span style="color: #DD0000">"userName"</span><span style="color: #007700">, </span><span style="color: #DD0000">"PWD"</span><span style="color: #007700">=></span><span style="color: #DD0000">"password"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$conn </span><span style="color: #007700">= </span><span style="color: #0000BB">sqlsrv_connect</span><span style="color: #007700">( </span><span style="color: #0000BB">$serverName</span><span style="color: #007700">, </span><span style="color: #0000BB">$connectionInfo</span><span style="color: #007700">);<br /><br />if( </span><span style="color: #0000BB">$conn </span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"Connection established.<br />"</span><span style="color: #007700">;<br />}else{<br /> echo </span><span style="color: #DD0000">"Connection could not be established.<br />"</span><span style="color: #007700">;<br /> die( </span><span style="color: #0000BB">print_r</span><span style="color: #007700">( </span><span style="color: #0000BB">sqlsrv_errors</span><span style="color: #007700">(), </span><span style="color: #0000BB">true</span><span style="color: #007700">));<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> <div class="example" id="example-2174"> <p><strong>Example #3 Connect on a specifed port.</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$serverName </span><span style="color: #007700">= </span><span style="color: #DD0000">"serverName\sqlexpress, 1542"</span><span style="color: #007700">; </span><span style="color: #FF8000">//serverName\instanceName, portNumber (default is 1433)<br /></span><span style="color: #0000BB">$connectionInfo </span><span style="color: #007700">= array( </span><span style="color: #DD0000">"Database"</span><span style="color: #007700">=></span><span style="color: #DD0000">"dbName"</span><span style="color: #007700">, </span><span style="color: #DD0000">"UID"</span><span style="color: #007700">=></span><span style="color: #DD0000">"userName"</span><span style="color: #007700">, </span><span style="color: #DD0000">"PWD"</span><span style="color: #007700">=></span><span style="color: #DD0000">"password"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$conn </span><span style="color: #007700">= </span><span style="color: #0000BB">sqlsrv_connect</span><span style="color: #007700">( </span><span style="color: #0000BB">$serverName</span><span style="color: #007700">, </span><span style="color: #0000BB">$connectionInfo</span><span style="color: #007700">);<br /><br />if( </span><span style="color: #0000BB">$conn </span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"Connection established.<br />"</span><span style="color: #007700">;<br />}else{<br /> echo </span><span style="color: #DD0000">"Connection could not be established.<br />"</span><span style="color: #007700">;<br /> die( </span><span style="color: #0000BB">print_r</span><span style="color: #007700">( </span><span style="color: #0000BB">sqlsrv_errors</span><span style="color: #007700">(), </span><span style="color: #0000BB">true</span><span style="color: #007700">));<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 notes" id="refsect1-function.sqlsrv-connect-notes"> <h3 class="title">Notes</h3> <p class="para"> By default, the <span class="function"><strong>sqlsrv_connect()</strong></span> uses connection pooling to improve connection performance. To turn off connection pooling (i.e. force a new connection on each call), set the "ConnectionPooling" option in the $connectionOptions array to 0 (or <strong><code>FALSE</code></strong>). For more information, see <a href="http://msdn.microsoft.com/en-us/library/cc644930.aspx" class="link external">» SQLSRV Connection Pooling</a>. </p> <p class="para"> The SQLSRV extension does not have a dedicated function for changing which database is connected to. The target database is specified in the $connectionOptions array that is passed to sqlsrv_connect. To change the database on an open connection, execute the following query "USE dbName" (e.g. sqlsrv_query($conn, "USE dbName")). </p> </div> <div class="refsect1 seealso" id="refsect1-function.sqlsrv-connect-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.sqlsrv-close.html" class="function" rel="rdfs-seeAlso">sqlsrv_close()</a> - Closes an open connection and releases resourses associated with the connection</span></li> <li class="member"> <span class="function"><a href="function.sqlsrv-errors.html" class="function" rel="rdfs-seeAlso">sqlsrv_errors()</a> - Returns error and warning information about the last SQLSRV operation performed</span></li> <li class="member"> <span class="function"><a href="function.sqlsrv-query.html" class="function" rel="rdfs-seeAlso">sqlsrv_query()</a> - Prepares and executes a query.</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.sqlsrv-configure.html">sqlsrv_configure</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.sqlsrv-errors.html">sqlsrv_errors</a></div> <div class="up"><a href="ref.sqlsrv.html">SQLSRV Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>