大网数据广告活动
PHP 语言与运行手册

odbc_connect · Connect to a datasource

· 52CE 我爱测

The connection id returned by this functions is needed by other ODBC functions. You can have multiple connections open at once as long as they either use different db or different credentials.

odbc_connect

(PHP 4, PHP 5, PHP 7, PHP 8)

odbc_connect — Connect to a datasource

说明

function odbc_connect(
    string $dsn,
    ?string $user = null,
    #[\SensitiveParameter] ?string $password = null,
    int $cursor_option = SQL_CUR_USE_DRIVER
): Odbc\Connection|false

The connection id returned by this functions is needed by other ODBC functions. You can have multiple connections open at once as long as they either use different db or different credentials.

With some ODBC drivers, executing a complex stored procedure may fail with an error similar to: "Cannot open a cursor on a stored procedure that has anything other than a single select statement in it". Using SQL_CUR_USE_ODBC may avoid that error. Also, some drivers don't support the optional row_number parameter in odbc_fetch_row(). SQL_CUR_USE_ODBC might help in that case, too.

参数

dsn

The database source name for the connection. Alternatively, a DSN-less connection string can be used.

user

The username. This parameter is ignored if dsn contains uid. To connect without specifying a user, use null.

password

The password. This parameter is ignored if dsn contains pwd. To connect without specifying a password, use null.

cursor_option

This sets the type of cursor to be used for this connection. This parameter is not normally needed, but can be useful for working around problems with some ODBC drivers.

The following constants are defined for cursortype:

  • SQL_CUR_USE_IF_NEEDED

  • SQL_CUR_USE_ODBC

  • SQL_CUR_USE_DRIVER

返回值

Returns an ODBC connection, 或者在失败时返回 false.

更新日志

版本 说明
8.4.0 此函数现在返回 Odbc\Connection 实例;之前返回 resource。
8.4.0 user and password are now nullable, they are now also optional and default to null.
8.4.0 Previously, using an empty string for password would not include pwd in the generated connection string for dsn. It is now generated to include a pwd which has an empty string as its value. To restore the previous behaviour password can now be set to null.
8.4.0 Previously, if dsn contained uid or pwd both user and password parameters were ignored. Now user is only ignored if dsn contains uid, and password is only ignored if dsn contains pwd.

示例

示例 #1 DSN-less connections

<?php
// Microsoft SQL Server using the SQL Native Client 10.0 ODBC Driver - allows connection to SQL 7, 2000, 2005 and 2008
$connection = odbc_connect("Driver={SQL Server Native Client 10.0};Server=$server;Database=$database;", $user, $password);

// Microsoft Access
$connection = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$mdbFilename", $user, $password);

// Microsoft Excel
$excelFile = realpath('C:/ExcelData.xls');
$excelDir = dirname($excelFile);
$connection = odbc_connect("Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq=$excelFile;DefaultDir=$excelDir" , '', '');
?>

参见

  • For persistent connections: odbc_pconnect() - Open a persistent database connection

来源:PHP Documentation Group · 作者与贡献记录 · CC BY 3.0。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
PHP Documentation Group
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。