php ODBC连接sqlserver

开放资料链接 (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);
?>



发表评论

邮箱地址不会被公开。