相关的函数

如下:

  • dba_close —关闭dba数据库
  • dba_delete —删除数据库中指定的key
  • dba_exists — 检查key是否存在
  • dba_fetch— 取得指定key的值
  • dba_firstkey — 获取第一个key
  • dba_handlers —列出所有可用的handler
  • dba_insert— 插入一条记录
  • dba_key_split— Splits a key in string representation into array representation
  • dba_list — 列出所有打开的数据库
  • dba_nextkey — 获取下一个key
  • dba_open —打开数据库
  • dba_optimize — 优化数据库
  • dba_popen — 使用长链接打开数据库
  • dba_replace —替换或者插入一条记录
  • dba_sync — 数据库同步

 

使用实例

1、跟踪用户和密码

<?php
$user = $_SERVER[\'argv\'][1];
$password = $_SERVER[\'argv\'][2];

$data_file = \'/tmp/users.db\';

$dbh = dba_open($data_file,\'c\',\'gdbm\') or die(\"Can\'t open db $data_file\");

if (dba_exists($user,$dbh)) {
    print \"User $user exists. Changing password.\";
} else {
    print \"Adding user $user.\";
}

dba_replace($user,$password,$dbh) or die(\"Can\'t write to data  $data_file\");

dba_close($dbh);
?>

<##ads_in_article_manong##>

2、对数据排序

<?php
$dbh = dba_open(\'users.db\',\'c\',\'gdbm\') or die($php_errormsg);

if ($exists = dba_exists($_POST[\'username\'], $dbh)) {
    $serialized_data = dba_fetch($_POST[\'username\'], $dbh) or die($php_errormsg);
    $data = unserialize($serialized_data);
} else {
    $data = array();
}

if ($_POST[\'new_password\']) {
    $data[\'password\'] = $_POST[\'new_password\'];
}
$data[\'last_access\'] = time();

if ($exists) {
    dba_replace($_POST[\'username\'],serialize($data), $dbh);
} else {
    dba_insert($_POST[\'username\'],serialize($data), $dbh);
}

dba_close($dbh);
?>

 

3、计算所有密码的总长度

<?php
$data_file = \'/tmp/users.db\';
$total_length = 0;
if (! ($dbh = dba_open($data_file,\'r\',\'gdbm\'))) {
    die(\"Can\'t open data  $data_file\");
}

$k = dba_firstkey($dbh);
while ($k) {
    $total_length += strlen(dba_fetch($k,$dbh));
    $k = dba_nextkey($dbh);
}

print \"Total length of all passwords is $total_length characters.\";

dba_close($dbh);
?>
收藏 打印