PHP8 odbc_connect

2024-03-21 08:59 更新

(PHP 4、PHP 5、PHP 7、PHP 8)

odbc_connect — 連接到數(shù)據(jù)源

說明

odbc_connect(
    string $dsn,
    string $user,
    string $password,
    int $cursor_option = SQL_CUR_USE_DRIVER
): resource|false

其他函數(shù)需要此函數(shù)返回的連接 ID ODBC 函數(shù)。您可以一次打開多個連接,只要滿足以下條件 它們要么使用不同的數(shù)據(jù)庫,要么使用不同的憑據(jù)。

對于某些 ODBC 驅動程序,執(zhí)行復雜的存儲過程可能會 失敗,并出現(xiàn)類似以下內容的錯誤:“無法在存儲的 包含單個 SELECT 語句以外的任何內容的過程 在里面”。使用SQL_CUR_USE_ODBC可以避免該錯誤。此外,一些 驅動程序不支持 odbc_fetch_row() 中的可選 row_number 參數(shù)。SQL_CUR_USE_ODBC可能會有所幫助 在那種情況下也是如此。

參數(shù) 

dsn

連接的數(shù)據(jù)庫源名稱?;蛘撸粋€ 可以使用無 DSN 的連接字符串。

user

用戶名。

password

密碼。

cursor_option

這將設置要使用的游標類型 對于此連接。通常不需要此參數(shù),但 可用于解決某些 ODBC 驅動程序的問題。

為 cursortype 定義了以下常量:
  • SQL_CUR_USE_IF_NEEDED
  • SQL_CUR_USE_ODBC
  • SQL_CUR_USE_DRIVER

返回值 

返回 ODBC 連接, 或者在失敗時返回 false.

示例 

示例 #1 無DSN連接

<?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" , '', '');
?>

參見 

  • 對于持久連接: odbc_pconnect() - 打開持久數(shù)據(jù)庫連接


以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號