PHP ODBC 資料庫連結函數庫

來源:互聯網
上載者:User

開放資料連結 (Open Database Connectivity,ODBC) 是連結資料庫的共通介面。ODBC 是由微軟主導的資料庫連結標準,實作環境也以微軟的系統最成熟。在 UNIX 系統中,通常要使用其它廠商所提供的 ODBC 介面,有些 UNIX 廠商會自己提供 ODBC 介面 (如 SUN 有為 Solaris 提供 ODBC)。
ODBC 和資料庫的查詢採用 SQL 語言,這和大部份的資料庫查詢方式一樣,這使得系統可以很容易和各種資料庫溝通。當然,透過 ODBC 介面,後端的資料庫不一定要 DBMS 這種大型資料庫系統,亦可以是資料表 (如 Microsoft Access)、或者是試算表 (如 Microsoft Excel)。

  odbc_autocommit: 開關自動改動功能。
  odbc_binmode: 配置二進位資料處理方式。
  odbc_close: 關閉 ODBC 連結。
  odbc_close_all: 關閉所有 ODBC 連結。
  odbc_commit: 改動 ODBC 資料庫。
  odbc_connect: 連結至 ODBC 資料庫。
  odbc_cursor: 取得遊標名。
  odbc_do: 執行 SQL 指令。
  odbc_exec: 執行 SQL 指令。
  odbc_execute: 執行預置 SQL 指令。
  odbc_fetch_into: 取得返回的指定列。
  odbc_fetch_row: 取得返回一列。
  odbc_field_name: 取得欄位名稱。
  odbc_field_type: 取得欄位資料類型。
  odbc_field_len: 取得欄位資料長度。
  odbc_free_result: 釋出返回資料的記憶體。
  odbc_longreadlen: 配置返回欄的最大值。
  odbc_num_fields: 取得欄位數目。
  odbc_pconnect: 長期連結至 ODBC 資料庫。
  odbc_prepare: 預置 SQL 指令。
  odbc_num_rows: 取得返回列數目。
  odbc_result: 取得返回資料。
  odbc_result_all: 返回 HTML 表格資料。
  odbc_rollback: 撤消當前交易。
  odbc_setoption: 調整 ODBC 配置。 

  odbc_autocommit 開關自動改動功能。
  文法: int odbc_autocommit(int connection_id, int [OnOff]);
  傳回值: 整數
  函數種類: 資料庫功能
  內容說明: 本函數用來開啟或關閉自動更新 (auto-commit) 功能。參數 connection_id 為 ODBC 連結的 ID 值。參數 OnOff 可省略,表開啟或關閉自動改動功能,預設值為 on,欲關閉則設 off。傳回值為目前的自動更新狀態,若開啟自動更新功能則返回 true;若關閉自動更新功能則返回 false。
  參考: odbc_commit() odbc_rollback()

  odbc_binmode 配置二進位資料處理方式。
  文法: int odbc_binmode(int result_id, int mode);
  傳回值: 整數
  函數種類: 資料庫功能
  內容說明: 本函數用來配置二進位資料返回時的處理方式。受到本函數影響的資料類型有 BINARY、VARBINARY 及 LONGVARBINARY。參數 result_id 為返回的 ID 值,若設為 0,則程式將配置一個新的 ID 值。

  參數 mode 為配置的二進位處理方式,有下列的值

    ODBC_BINMODE_PASSTHRU 返回二進位資料
    ODBC_BINMODE_RETURN 轉成十六進位返回
    ODBC_BINMODE_CONVERT 轉成字串資料返回  

  注意: 若使用 odbc_fetch_into(),ODBC_BINMODE_PASSTHRU 會導至返回字串值都是空的 (empty)。而返回資料最大治募預設大小為 4096 位組,若要返回更多的資料需使用 odbc_longreadlen()。
  參考: odbc_fetch_into() odbc_longreadlen()

  odbc_close 關閉 ODBC 連結。
  文法: void odbc_close(int connection_id);
  傳回值: 無
  函數種類: 資料庫功能
  內容說明: 本函數用來關閉與 ODBC 資料庫之間的連結。參數 connection_id 為 ODBC 連結代號 ID 值。若正在執行 transactions 則無法關閉連結。

  odbc_close_all 關閉所有 ODBC 連結。
  文法: void odbc_close_all(void);
  傳回值: 無
  函數種類: 資料庫功能
  內容說明: 本函數用來關閉與 ODBC 資料庫之間所有的連結。和 odbc_close() 一樣,若正在執行 transactions 則無法關閉連結。使用本函數不需要使用任何參數。

  odbc_commit 改動 ODBC 資料庫。
  文法: int odbc_commit(int connection_id);
  傳回值: 整數
  函數種類: 資料庫功能
  內容說明: 本函數用來改動 (commit) ODBC 資料庫。參數 connection_id 為 ODBC 連結的 ID 值。若有配置 odbc_autocommit() 成自動改動資料庫時,則不需使用本函數。

  odbc_connect 連結至 ODBC 資料庫。
  文法: int odbc_connect(string dsn, string user, string password, int [cursor_type]);
  傳回值: 整數
  函數種類: 資料庫功能
  內容說明: 本函數用來連結到 ODBC 資料庫,傳回值為連結的代號 ID 值。參數 dsn 為資料來源名稱 (Data Sources Name)。參數 user 及 password 分別為連結的帳號及密碼。參數 cursor_type 通常省略,其值有 SQL_CUR_USE_IF_NEEDED、SQL_CUR_USE_ODBC、SQL_CUR_USE_DRIVER 及 SQL_CUR_DEFAULT 等四種。當使用複雜的資料存取時可能會有類似 "Cannot open a cursor on a stored procedure that has anything other than a single select statement in it" 的錯誤資訊字串,此時若將參數 cursor_type 的值設為 SQL_CUR_USE_ODBC 就可以避開了。
  參考: odbc_pconnect()

  odbc_cursor 取得遊標名。
  文法: string odbc_cursor(int result_id);
  傳回值: 字串
  函數種類: 資料庫功能
  內容說明: 本函數用來取得開啟的 ODBC 資料庫遊標 (cursor)。參數 connection_id 為 ODBC 連結代號 ID 值。傳回值為遊標的名稱字串。

  odbc_do 執行 SQL 指令。
  文法: int odbc_do(int connection_id, string query);
  傳回值: 整數
  函數種類: 資料庫功能
  內容說明: 本函數用來執行 SQL 的 query 指令。參數 connection_id 為 ODBC 連結代號 ID 值。參數 query 則為欲執行的指令。傳回值為 Result ID 值。
  參考: odbc_prepare() odbc_execute() odbc_exec()

  odbc_exec 執行 SQL 指令。
  文法: int odbc_exec(int connection_id, string query);
  傳回值: 整數
  函數種類: 資料庫功能
  內容說明: 本函數用來執行 SQL 的 query 指令。參數connection_id 為 ODBC 連結代號 ID 值。參數 query 則為欲執行的指令。傳回值為 Result ID 值。
  參考: odbc_prepare() odbc_execute() odbc_do()

  odbc_execute 執行預置 SQL 指令。
  文法: int odbc_execute(int result_id, array [parameters_array]);
  傳回值: 整數
  函數種類: 資料庫功能
  內容說明: 本函數用來執行預置在4.36.20 odbc_prepare() 的 query 指令。參數 result_id 為 ODBC 返回 ID 值。參數 parameters_array 通常省略。

  使用範例
  <?php
    $conn=odbc_connect("mydb","","");
    $stmt = odbc_prepare($conn, "INSERT INTO mytable (jor_from, jor_to) valueS('$from', '$to');" );
    if (!odbc_execute($stmt)) {
      echo "錯誤";
    }
    odbc_close($conn);
  ?> 
  參考: odbc_prepare() odbc_exec() odbc_do()

  odbc_fetch_into 取得返回的指定列。
  文法: int odbc_fetch_into(int result_id, int [rownumber], array result_array);
  傳回值: 整數
  函數種類: 資料庫功能
  內容說明: 本函數用來取得 Query 的返回資料列,並放入數組資料之中。參數 result_id 為 ODBC 返回 ID 值。參數 rownumber 為指定的取得列 (row)。數組參數 result_array 為取得列的資料數組,通常在前面加上 "&" 符號。例如 vbhunt@silverfox.com 所提供的範例 (27-Sep-1998)
$cols = odbc_fetch_into($QueryID, $RowNum, &$YourArray);

odbc_fetch_row 取得返回一列。
  文法: int odbc_fetch_row(int result_id, int [row_number]);
  傳回值: 整數
  函數種類: 資料庫功能
  內容說明: 本函數用來取得 Query 的返回資料的一列。參數 result_id 為 ODBC 返回 ID 值。參數 rownumber 可省略,為指定的取得列 (row)。

  使用範例
  <?php
    $conn = odbc_connect("WebDB", "", "");
    $query = "select user, idno ";
    $query .= "from userinfo";
    $result_id = odbc_do($conn, $query);
    while(odbc_fatch_row($result_id)) {
      $user = odbc_result($result_id, 1);
      $idno = odbc_result($result_id, 2);
      echo $user."的身份證字型大小為".$idno."<br>n";
    }
    odbc_close($conn);
  ?> 

  odbc_field_name 取得欄位名稱。
  文法: string odbc_fieldname(int result_id, int field_number);
  傳回值: 字串
  函數種類: 資料庫功能
  內容說明: 本函數用來取得 Query 的返回資料的欄位名稱。參數 result_id 為 ODBC 返回 ID 值。參數 field_number 為指定的欄位,起始值為 1。

  odbc_field_type 取得欄位資料類型。
  文法: string odbc_field_type(int result_id, int field_number);
  傳回值: 字串
  函數種類: 資料庫功能
  內容說明: 本函數用來取得 Query 的返回資料的欄位資料類型。參數 result_id 為 ODBC 返回 ID 值。參數 field_number 為指定的欄位,起始值為 1。

  odbc_field_len 取得欄位資料長度。
  文法: int odbc_field_len(int result_id, int field_number);
  傳回值: 整數
  函數種類: 資料庫功能
  內容說明: 本函數用來取得 Query 的返回資料的欄位資料長度。參數 result_id 為 ODBC 返回 ID 值。參數 field_number 為指定的欄位,起始值為 1。

  odbc_free_result 釋出返回資料的記憶體。
  文法: int odbc_free_result(int result_id);
  傳回值: 整數
  函數種類: 資料庫功能
  內容說明: 本函數可用來釋出返回資料所使用的記憶體,通常在系統記憶體資源不足的情形下才需要使用。參數 result_id 為 ODBC 返回 ID 值。傳回值永遠都是 true。

  odbc_longreadlen 配置返回欄的最大值。
  文法: int odbc_longreadlen(int result_id, int length);
  傳回值: 整數
  函數種類: 資料庫功能
  內容說明: 本函數用來配置返回資料欄的最大資料值。參數 result_id 為 ODBC 返回 ID 值。參數 length 即為欲配置的長度值,本參數若設為 0,表示不限長度。

  odbc_num_fields 取得欄位數目。
  文法: int odbc_num_fields(int result_id);
  傳回值: 整數
  函數種類: 資料庫功能
  內容說明: 本函數用來取得返回資料的欄位數目。參數 result_id 為 ODBC 返回 ID 值。若發生錯誤則返回 -1。

  odbc_pconnect 長期連結至 ODBC 資料庫。
  文法: int odbc_pconnect(string dsn, string user, string password, int [cursor_type]);
  傳回值: 整數
  函數種類: 資料庫功能
  內容說明: 本函數用來保持長期網站連結接到 ODBC 資料庫,即使 PHP 程式結束,連結仍然存在,傳回值為連結的代號 ID 值。這對連結負責很重的系統可以加快連結的速度。參數 dsn 為資料來源名稱 (Data Sources Name)。參數 user 及 password 分別為連結的帳號及密碼。參數 cursor_type 通常省略。
  參考: odbc_connect()

  odbc_prepare 預置 SQL 指令。
  文法: int odbc_prepare(int connection_id, string query_string);
  傳回值: 整數
  函數種類: 資料庫功能
  內容說明: 本函數用來配置 SQL query 指令。參數 result_id 為 ODBC 返回 ID 值。參數 query_string 即為預設的指令。
  參考: odbc_execute()

  odbc_num_rows 取得返回列數目。
  文法: int odbc_num_rows(int result_id);
  傳回值: 整數
  函數種類: 資料庫功能
  內容說明: 本函數用來取得返回列的數目。參數 result_id 為 ODBC 返回 ID 值。若有錯誤發生則返回 -1。

  odbc_result 取得返回資料。
  文法: string odbc_result(int result_id, mixed field);
  傳回值: 字串
  函數種類: 資料庫功能
  內容說明: 本函數用來取得返回的資料。參數 result_id 為 ODBC 返回 ID 值。參數 field 可以是數字類型,代表指定的欄位序號 (從 1 開始);亦可以是字串類型,為指定的欄位名稱。

  odbc_result_all 返回 HTML 表格資料。
  文法: int odbc_result_all(int result_id, string [format]);
  傳回值: 整數
  函數種類: 資料庫功能
  內容說明: 本函數用來將取得的資料轉成 HTML 的表格 (table) 格式。參數 result_id 為 ODBC 返回 ID 值。參數 format 可省略,為表格的特殊配置值。傳回值為資料列數。

  使用範例
  <?php
    $conn = odbc_connect("WebDB", "", "");
    $query = "select item, price ";
    $query .= "from userinfo";
    $result_id = odbc_do($conn, $query);
    odbc_result_all($result_id, "border=1 width=50%");
    odbc_close($conn);
  ?>

  odbc_rollback 撤消當前交易。
  文法: int odbc_rollback(int connection_id);
  傳回值: 整數
  函數種類: 資料庫功能
  內容說明: 本函數取消 ODBC 交易處理 (transaction) 對資料庫所做的修改。若成功則返回 true,反之返回 false。

  odbc_setoption 調整 ODBC 配置。
  文法: int odbc_setoption(int id, int function, int option, int param);
  傳回值: 整數
  函數種類: 資料庫功能
  內容說明: 本函數用來調整 ODBC 的配置值。參數 id 不一定指 connection_id,SQLSetConnectOption() 則為 connection_id;若為 SQLSetStmtOption() 則為 result_id。參數 function 值為 1 表 SQLSetConnectOption();若為 2 則為 SQLSetStmtOption()。參數 option 為配置的選項。參數 param 為配置值。

  使用範例
  <?
    // 例一: 配置為 autocommit,即與 odbc_autocommit($conn, true) 相同。
    // 第三個參數值 102 表 SQL_AUTOCOMMIT。
    // 第四個參數值 1 表 SQL_AUTOCOMMIT_ON。
    odbc_setoption ($conn, 1, 102, 1);
    // 例二: 配置查詢時間
    // 第三個參數 0 表 SQL_QUERY_TIMEOUT
    // 第四個參數為最久執行時間,本例設為 30 秒
    $result = odbc_prepare ($conn, $sql);
    odbc_setoption ($result, 2, 0, 30);
    odbc_execute ($result);
  ?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.