sqlsrv_connect

来源:http://msdn.microsoft.com/en-us/library/cc296161(v=sql.105).aspx


SQL Server 2008 R2
This topic has not yet been rated Rate this topic

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.

Beginning in version 3.0 of the Microsoft Drivers for PHP for SQL Server, you can also specify a LocalDB instance with "(localdb)\instancename". For more information, see PHP Driver for SQL Server Support for LocalDB.

Also beginning in version 3.0 of the Microsoft Drivers for PHP for SQL Server, you can specify a virtual network name, to connect to an AlwaysOn availability group. For more information about Microsoft Drivers for PHP for SQL Server support for AlwaysOn Availability Groups, see PHP Driver for SQL Server Support for High Availability, Disaster Recovery.

$connectionInfo [OPTIONAL]: An associative array that contains connection attributes (for example, array("Database" => "AdventureWorks")). See Connection Options for a list of the supported keys for the array.

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, "Database"=>"AdventureWorks");
*/
$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);
?>
Concepts
Other Resources



sqlsrv_connect

sqlsrv_connect — Opens a connection to a Microsoft SQL Server database

reject note 说明

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

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.

reject note 参数

serverName

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).

connectionInfo

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 » SQLSRV Connection Options.

reject note 返回值

A connection resource. If a connection cannot be successfully opened, FALSE is returned.

reject note 范例

Example #1 Connect using Windows Authentication.

<?php
$serverName 
"serverName\sqlexpress"//serverName\instanceName

// Since UID and PWD are not specified in the $connectionInfo array,
// The connection will be attempted using Windows Authentication.
$connectionInfo = array( "Database"=>"dbName");
$conn sqlsrv_connect$serverName$connectionInfo);

if( 
$conn ) {
     echo 
"Connection established.<br />";
}else{
     echo 
"Connection could not be established.<br />";
     die( 
print_rsqlsrv_errors(), true));
}
?>

Example #2 Connect by specifying a user name and password.

<?php
$serverName 
"serverName\sqlexpress"//serverName\instanceName
$connectionInfo = array( "Database"=>"dbName""UID"=>"userName""PWD"=>"password");
$conn sqlsrv_connect$serverName$connectionInfo);

if( 
$conn ) {
     echo 
"Connection established.<br />";
}else{
     echo 
"Connection could not be established.<br />";
     die( 
print_rsqlsrv_errors(), true));
}
?>

Example #3 Connect on a specifed port.

<?php
$serverName 
"serverName\sqlexpress, 1542"//serverName\instanceName, portNumber (default is 1433)
$connectionInfo = array( "Database"=>"dbName""UID"=>"userName""PWD"=>"password");
$conn sqlsrv_connect$serverName$connectionInfo);

if( 
$conn ) {
     echo 
"Connection established.<br />";
}else{
     echo 
"Connection could not be established.<br />";
     die( 
print_rsqlsrv_errors(), true));
}
?>

reject note 注释

By default, the sqlsrv_connect() 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 FALSE). For more information, see» SQLSRV Connection Pooling.

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")).

reject note 参见

  • sqlsrv_close() - Closes an open connection and releases resourses associated with the connection
  • sqlsrv_errors() - Returns error and warning information about the last SQLSRV operation performed
  • sqlsrv_query() - Prepares and executes a query.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
最近帮朋友改一个小东西的时候才发现的,微软专门为PHP出了个SQL Server的扩展(当然是Windows版本的),用了一下发现还是很好用的,对于Windows下使用php开发SQL Server应用来说,这个扩展有利于兼容性以及充分利用SQL Server特性。 PHP本身有个php_mssql.dll的扩展用来连接Sql server,但是貌似这个dll只是用来连接低版本 Sql server的(2000以下版本),在Sql server 2005以后版本则根本无法使用mssql_connect连接到数据库。 先到微软网站下载 SQL Server Driver for PHP :http://www.microsoft.com/downloads/details.aspx?familyid=CCDF728B-1EA0-48A8-A84A-5052214CAAD9&displaylang=en , 这是一个自解压的EXE文件,解压缩后你会得到这么几个文件: 其中的52、53表示就是php的5.2.x和5.3.x 版本,你必须选择跟你php版本相匹配的; vc6、vc9表示的是编译这个dll所使用的vc++编译器版本,基本上大多数时候,选vc6的; nts、ts表示的是否是 ThreadSafe的,得根据安装的php版本来选择,如果你不确定,就两种情况分别尝试一下好了; 我服务器上安装的是 ThreadSafe版本的php-5.2.x,所以选择的是php_sqlsrv_52_nts_vc6.dll,把这个文件拷贝到php的ext目录,比如: 1 C:\php\ext 然后修改php.ini在适当的地方加上一行: 1 extension=php_sqlsrv_52_nts_vc6.dll 然后重启web服务器就可以了。 运行一个phpinfo看看: 另外需要注意的是,如果使用这个扩展连接Sql server 2005以上版本的sql server,你还需要在机器上先安装 SQL Server Native Client :http://download.microsoft.com/download/0/E/6/0E67502A-22B4-4C47-92D3-0D223F117190/sqlncli.msi 这个扩展为php新增了一系列sqlsrv_开头的函数, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 sqlsrv_begin_transaction sqlsrv_cancel sqlsrv_client_info sqlsrv_close sqlsrv_commit sqlsrv_configure sqlsrv_connect sqlsrv_errors sqlsrv_execute sqlsrv_fetch sqlsrv_fetch_array sqlsrv_fetch_object sqlsrv_fetch_metadata sqlsrv_free_stmt sqlsrv_get_config sqlsrv_get_field sqlsrv_has_rows sqlsrv_next_result sqlsrv_num_fields sqlsrv_num_rows sqlsrv_prepare sqlsrv_query sqlsrv_rollback sqlsrv_rows_affected sqlsrv_send_stream_data sqlsrv_server_info 详细的使用文档可以在之前下载的自解压文件里面找到。 摘抄一段文档里面的例子: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 /*===================================================================== This file is part of a Microsoft SQL Server Shared Source Application. Copyright (C) Microsoft Corporation. All rights reserved. THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. ======================================================= */ $serverName = "(local)"; $connectionInfo = array( "Database"=>"AdventureWorks"); /* Connect using Windows Authentication. */ $conn = sqlsrv_connect( $serverName, $connectionInfo); if( $conn === false ) { echo "Could not connect.\n"; die( print_r( sqlsrv_errors(), true)); } /* Get the product picture for a given product ID. */ $tsql = "SELECT LargePhoto FROM Production.ProductPhoto AS p JOIN Production.ProductProductPhoto AS q ON p.ProductPhotoID = q.ProductPhotoID WHERE ProductID = ?"; $params = array($_REQUEST['productId']); /* Execute the query. */ $stmt = sqlsrv_query($conn, $tsql, $params); if( $stmt === false ) { echo "Error in statement execution.</br>"; die( print_r( sqlsrv_errors(), true)); } /* Retrieve the image as a binary stream. */ if ( sqlsrv_fetch( $stmt ) ) { $image = sqlsrv_get_field( $stmt, 0, SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY)); fpassthru($image); } else { echo "Error in retrieving data.</br>"; die(print_r( sqlsrv_errors(), true)); } /* Free the statement and connectin resources. */ sqlsrv_free_stmt( $stmt ); sqlsrv_close( $conn ); 是不是很简单?跟平时用的mysql、mssql函数差不多的。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值