发新话题
打印

PHP4手册:函数库及函数(十) dbm 类资料库函式库

本主题由 admin 于 2007-11-4 12:03 移动

PHP4手册:函数库及函数(十) dbm 类资料库函式库

--------------------------------------------------------------------------------
dbm 类资料库函式库  
--------------------------------------------------------------------------------
  

本函式库共有 10 个函式
dbm 为柏克莱大学所发展的档案型资料库。欲使用本函式可能需要先安装相关的 DBM 函数馆,当然在 BSD 系列的作业系统中已经装好的就不用自行动手了。
dbm 在资料存取没有很复杂的栏位,它只有单纯的键/值 (key/value) 的栏位。
下例为简单的 dbm 使用范例
function do_stuff() {
  // 略去复杂的资料处理流程
}
$dbm = dbmopen("lastseen", "w");
if (dbmexists($dbm, $userid)) {
  $last_seen = dbmfetch($dbm, $userid);
} else {
  dbminsert($dbm, $userid, time());
}
do_stuff();
dbmreplace($dbm, $userid, time());
dbmclose($dbm);
?>


dbmopen: 开启 DBM 资料库连结。  
dbmclose: 关闭 DBM 资料库。  
dbmexists: 检查键是否存在。  
dbmfetch: 取回指定资料。  
dbminsert: 加入资料。  
dbmreplace: 更动或加入资料。  
dbmdelete: 删除指定资料。  
dbmfirstkey: 取回首笔键名。  
dbmnextkey: 取回下笔键值。  
dblist: 取得 DBM 的资讯。  

--------------------------------------------------------------------------------
函式:dbmopen()  
--------------------------------------------------------------------------------
  

dbm 类资料库函式库

dbmopen
开启 DBM 资料库连结。
语法: int dbmopen(string filename, string flags);
传回值: 整数
函式种类: 资料库功能

  
  
内容说明  

本函式用来开启指定的资料库。参数 path 为资料库的路径加上资料库名称。参数 mode 值如下表
属性 说明  
r 开启唯读既有资料库  
w 开启可读写既有资料库  
c 开启可读写资料库,若不存在则建立  
n 删去现有资料库,若不存在则建立,之后可读写  

传回值为资料库的代号 ID,若失败则传回 false。  

--------------------------------------------------------------------------------
函式:dbmclose()  
--------------------------------------------------------------------------------
  

dbm 类资料库函式库

dbmclose
关闭 DBM 资料库。
语法: boolean dbmclose(int handle);
传回值: 布林值
函式种类: 资料库功能

  
  
内容说明  

本函式用来将已开启的资料库关闭。参数 handle 为开启资料库时所传回来的代号 ID。

--------------------------------------------------------------------------------
函式:dbmexists()  
--------------------------------------------------------------------------------
  

dbm 类资料库函式库

dbmexists
检查键是否存在。
语法: boolean dbmexists(int handle, string key);
传回值: 布林值
函式种类: 资料库功能

  
  
内容说明  

本函式用来检查指定的键是否存在。参数 handle 为开启资料库时所传回来的代号 ID。参数 key 为待检查的键值 (key)。若键存在则传回 true 值。

--------------------------------------------------------------------------------
函式:dbmfetch()  
--------------------------------------------------------------------------------
  

dbm 类资料库函式库

dbmfetch
取回指定资料。
语法: string dbmfetch(int handle, string key);
传回值: 字串
函式种类: 资料库功能

  
  
内容说明  

本函式取得指定的资料。参数 handle 为开启资料库时所传回来的代号 ID。参数 key 为欲取出资料的键值 (key)。传回值即为资料字串,若取出失败则传回 false。

--------------------------------------------------------------------------------
函式:dbminsert()  
--------------------------------------------------------------------------------
  

dbm 类资料库函式库

dbminsert
加入资料。
语法: int dbminsert(int handle, string key, string value);
传回值: 整数
函式种类: 资料库功能

  
  
内容说明  

本函式将加入资料至资料库中。参数 handle 为开启资料库时所传回来的代号 ID。参数 key 为键值 (key) 字串。参数 value 为欲加入的资料内容。成功则传回 0;传回值 -1 表示该资料库档案为唯读的状态;传回值 1 则表示该键已经存在,可用 dbmreplace() 取代。

--------------------------------------------------------------------------------
函式:dbmreplace()  
--------------------------------------------------------------------------------
  

dbm 类资料库函式库

dbmreplace
更动或加入资料。
语法: boolean dbmreplace(int handle, string key, string value);
传回值: 布林值
函式种类: 资料库功能

  
  
内容说明  

本函式更动资料库中的资料,若资料不存在则加入。参数 handle 为开启资料库时所传回来的代号 ID。参数 key 为键值 (key) 字串。参数 value 为欲更动的资料内容。成功则传回 true 值。

--------------------------------------------------------------------------------
函式:dbmdelete()  
--------------------------------------------------------------------------------
  

dbm 类资料库函式库

dbmdelete
删除指定资料。
语法: boolean dbmdelete(int handle, string key);
传回值: 布林值
函式种类: 资料库功能

  
  
内容说明  

本函式将删除指定的资料。参数 handle 为开启资料库时所传回来的代号 ID。参数 key 为欲删除的键值 (key)。若该键值不存在无法删除则传回 false
  
--------------------------------------------------------------------------------
函式:dbmfirstkey()  
--------------------------------------------------------------------------------
  

dbm 类资料库函式库

dbmfirstkey
取回首笔键名。
语法: string dbmfirstkey(int handle);
传回值: 字串
函式种类: 资料库功能

  
  
内容说明  

本函式取得资料库的第一笔键名 (key)。参数 handle 为开启资料库时所传回来的代号 ID。传回值即为键名,若取出失败则传回 false。值得注意的是资料库的资料没有任何排序的情形,无法预期传回的资料是依何方式排序后的第一笔资料。若需要特定顺序的第一笔资料,则需读回全部的资料到阵列中,再使用 PHP 的排序函式排序后方可取得所需值。

--------------------------------------------------------------------------------
函式:dbmnextkey()  
--------------------------------------------------------------------------------
  

dbm 类资料库函式库

dbmnextkey
取回下笔键值。
语法: string dbmnextkey(int handle, string key);
传回值: 字串
函式种类: 资料库功能

  
  
内容说明  

本函式取得资料库的下一笔键值 (key)。参数 handle 为开启资料库时所传回来的代号 ID。传回值即为键值。

  
  
使用范例  

// 之前已经开启 DBM 资料库。
$key = dbmfirstkey($dbm_id);
while ($key) {
    echo "$key = " . dbmfetch($dbm_id, $key) . "\n";
    $key = dbmnextkey($dbm_id, $key);
}
?>  
  
--------------------------------------------------------------------------------
函式:dblist()  
--------------------------------------------------------------------------------
  

dbm 类资料库函式库

dblist
取得 DBM 的资讯。
语法: string dblist(void);
传回值: 字串
函式种类: 资料库功能

  
  
内容说明  

本函式取得 DBM 资料库函式馆的相关资讯。本函式不需输入参数,传回值为资讯字串。

TOP

发新话题