sqlsrv_connect

2024-01-27 09:38
文章标签 connect sqlsrv

本文主要是介绍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.

 Syntax

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

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

 Return Value

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

 Remarks

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.

 Example

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);
?>
 See Also

Concepts
About Code Examples in the Documentation
Other Resources
SQLSRV Driver API Reference
Connecting to the Server



来源:http://www.php.net/manual/zh/function.sqlsrv-connect.php

sqlsrv_connect

sqlsrv_connect — Opens a connection to a Microsoft SQL Server database

Report a bug

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.

Report a bug

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.

Report a bug

reject note 返回值

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

Report a bug

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));
}
?>

Report a bug

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

Report a bug

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.

这篇关于sqlsrv_connect的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/649840

相关文章

mysql出现ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost‘ (10061)的解决方法

《mysql出现ERROR2003(HY000):Can‘tconnecttoMySQLserveron‘localhost‘(10061)的解决方法》本文主要介绍了mysql出现... 目录前言:第一步:第二步:第三步:总结:前言:当你想通过命令窗口想打开mysql时候发现提http://www.cpp

Debugging Lua Project created in Cocos Code IDE creates “Waiting for debugger to connect” in Win-7

转自 I Installed Cocos Code IDE and created a new Lua Project. When Debugging the Project(F11) the game window pops up and gives me the message waiting for debugger to connect and then freezes. Also a

Oracle start with connect BY 死循环

解决办法 检查start with前有没有where条件, 如果有的话,套一层select,再 Oracle start with connect BY

ERROR 2003 (HY000): Can't connect to MySQL server on (10061)

在linux系统上装了一个mysql-5.5,启动后本机都是可以访问的,操作都正常,同时建了一个%的用户(支持远程访问), root@debian:/# mysql -u loongson -pEnter password: Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id

Docker容器创建时,无法访问镜像源:Could not connect to archive.ubuntu.com:80

1.问题描述 当基于dockerfile创建容器时,遇到Could not connect to ...、Failed to fetch ...等异常时,大概原因是没有配置好容器创建所需的镜像源。这里以Ubuntu基础镜像源为例。 dockerfile内容 FROM ubuntuRUN apt update && apt install python3 -y && apt install

API 网关 OpenID Connect 实战:单点登录(SSO)如此简单

作者:戴靖泽,阿里云 API 网关研发,Higress 开源社区 Member 前言 随着企业的发展,所使用的系统数量逐渐增多,用户在使用不同系统时需要频繁登录,导致用户体验较差。单点登录(Single Sign-On,简称 SSO)正是为了解决这一问题。当用户登录一次后,即可获取所有系统的访问权限,不需要对每个单一系统逐一登录。 目前,SSO 的实现方案常见有以下几种: 基于 JWT:

Object::connect: No such slot

信号槽出现这样的问题一定要注意以下几点: ThreadFromQThread work_download ; QObject::connect(this, SIGNAL(send_down_sig(int)),\ &work_download, SLOT(recv_down_info(int))); 注意槽函数仅仅是填

ubuntu下qt编译显示Cannot connect creator comm socket /tmp/qt_temp.xxx/stub-socket的解决办法

Cannot connect creator comm socket /tmp/qt_temp.u14973/stub-socket: No such file or directory   于是查了一下,知道qt不能用gnome-terminal执行,需要使用x-team,于是想到了解决方法: 找到:工具(Tools)->环境(environment)->系统(Sy

rl: (7) Failed to connect to get.rvm.io port 443: Operation timed out

问题:curl -L https://get.rvm.io | bash -s stable 命令失败 报错内容: rl: (7) Failed to connect to get.rvm.io port 443: Operation timed out 或 curl: (35) LibreSSL SSL_connect: SSL_ERROR_SYSCALL in connection t

QT5中connect函数无法触发槽函数

QT5中connect函数无法触发槽函数 趁着加班的时候,没事玩会QT5,结果发现connect链接一个button和clicked信号的槽函数,调试运行后,点击按钮一直无法触发槽函数。各种方法都试过了,最后才发现是个弱智问题。 以下代码为有问题代码: connect(button1, SIGNAL(clicked()), this, SLOT(showArea)); 以下为修改后代码: