How to: Connect on a Specified Port

Microsoft Drivers for PHP for SQL Server

Collapse image Expand Image Copy image CopyHover image

This topic describes how to connect to SQL Server on a specified port with the Microsoft Drivers for PHP for SQL Server.

To connect on a specified port

  1. Verify the port on which the server is configured to accept connections. For information about configuring a server to accept connections on a specified port, see How to: Configure a Server to Listen on a Specific TCP Port (SQL Server Configuration Manager).

  2. Add the desired port to the $serverName parameter of the sqlsrv_connect function. Separate the server name and the port with a comma. For example, the following lines of code use the SQLSRV driver to demonstrate how to connect to a server named myServer on port 1521:

      Copy imageCopy Code
    $serverName = "myServer, 1521";
    sqlsrv_connect( $serverName );

    The following lines of code use the PDO_SQLSRV driver to demonstrate how to connect to a server named myServer on port 1521:

      Copy imageCopy Code
    $serverName = "(local), 1521";
    $database = "AdventureWorks";
    $conn = new PDO( "sqlsrv:server=$serverName;Database=$database", "", "");

See Also