php dba,php dba函数之dba_open()使用讲解

dba_open()介绍

语法:

resource dba_open ( string $path , string $mode [, string $handler [, mixed $... ]])

参数:

string $path :打开数据库所在的目录。

string $mode : 打开的模式。第一个字符位置,'r’:读的方式; 'w’:写的方式; 'c’:读写方式,如果数据库不存在,则创建; 'n’:创建,以读写方式;第二个字符位置,'l’:以锁定的方式,并生成一个.lck的文件; 'd’:锁定数据库自己。第三个字符位置:'t’:测试访问锁而且不想等待的时候,用此选项。

注意:对一个数据库文件,只能有一个人可以写操作。当dba数据库用在web服务或者多个需要写操作的时候,只能是一个接着一个,不能同时写,而且在写的时候,读也是不允许的。dba的扩展用锁来防止同时操作,请看下表:

DBA locking

already open

mode = "rl"

mode = "rlt"

mode = "wl"

mode = "wlt"

mode = "rd"

mode = "rdt"

mode = "wd"

mode = "wdt"

not open

ok

ok

ok

ok

ok

ok

ok

ok

mode = "rl"

ok

ok

wait

false

illegal

illegal

illegal

illegal

mode = "wl"

wait

false

wait

false

illegal

illegal

illegal

illegal

mode = "rd"

illegal

illegal

illegal

illegal

ok

ok

wait

false

mode = "wd"

illegal

illegal

illegal

illegal

wait

false

wait

false

介绍:

ok: the second call will be successfull. 第二次调用将会成功

wait: the second call waits until dba_close() is called for the first. 第二次调用会等待,直到调用dba_close() 时候

false: the second call returns false. 第二次调用会返回false

illegal: you must not mix "l" and "d" modifiers for mode parameter.'l' 和 'd'禁止混合使用在模式参数中

string $handler:使用的数据库

返回值:

成功返回handler, 失败返回 false

dba_open()实例

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

$total_length = 0;

if (! ($dbh = dba_open($data_file,'r','gdbm'))) {

die("无法打开数据库$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);

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值