dba_open

(PHP 4, PHP 5, PHP 7, PHP 8)

dba_open打开数据库

说明

dba_open(
    string $path,
    string $mode,
    ?string $handler = null,
    int $permission = 0644,
    int $map_size = 0,
    ?int $flags = null
): Dba\Connection|false

dba_open()modehandlerpath 建立一个数据库实例。

参数

path

数据库文件的路径。通常是文件系统中的一个常规路径。

mode

一个字符串,指定数据库的访问模式。它是一个字符,可以是 r 用于读取访问, w 用于对已存在的数据库进行读写访问, c 用于读写访问和数据,如果目前不存在,则创建数据库, 以及 n 用于创建、截断和读写访问。 数据库是以 BTree 模式创建的,不支持其他模式(如 Hash 或 Queue)。

此外,您可以使用下一个字符设置数据库锁定方法。使用 l.lck 文件锁定数据库,或使用 d 锁定数据库文件本身。重要的是您的所有应用程序都要一致地这样做。

如果您想测试访问并且不想等待锁定,可以将 t 作为第三个字符添加。当您确信不需要数据库锁定时, 可以使用 - 代替 ld。当 dl- 都没有使用时,dba 将锁定数据库文件,就像使用 d 一样。

注意:

一个数据库文件只能有一个写入者。当您在 web 服务器上使用 dba 时,如果有多个请求需要写入操作, 它们只能一个接一个地进行。在写入时不允许读取。dba 扩展使用锁来防止这种情况。请参阅下表:

DBA 锁定
已经打开 mode = "rl" mode = "rlt" mode = "wl" mode = "wlt" mode = "rd" mode = "rdt" mode = "wd" mode = "wdt"
未打开 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: 第二次调用将成功。
  • wait: 第二次调用将等待,直到第一个调用 dba_close()
  • false: 第二次调用返回 false。
  • illegal: 不能混合 "l""d" 修饰符用于 mode 参数。

handler

用于访问 path处理器 的名称。 它接收所有给定给 dba_open() 的可选参数, 并可以代表它们执行操作。如果 handlernull,则调用默认处理器。

permission

可选的 int 参数,传递给驱动程序。它的含义与 chmod()permissions 参数相同, 默认为 0644

db1db2db3db4dbmgdbmndbmlmdb 驱动程序支持 permission 参数。

map_size

可选的 int 参数,传递给驱动程序。它的值应该是 OS 的页面大小的倍数,或者是零,以使用默认的映射大小。

只有 lmdb 驱动程序接受 map_size 参数。

flags

传递给数据库驱动程序的标志。如果 null,将提供默认标志。 目前,只有 LMDB 驱动程序支持以下标志 DBA_LMDB_USE_SUB_DIRDBA_LMDB_NO_SUB_DIR

返回值

成功时返回一个 Dba\Connection 实例, 或者在失败时返回 false

错误/异常

如果 handlernull,但没有默认处理器,则返回 false 并发出一个 E_WARNING 级别的错误。

更新日志

版本 说明
8.4.0 现在返回一个 Dba\Connection 实例; 以前返回一个 resource
8.2.0 flags 参数被添加。
8.2.0 handler 现在可以为 null
7.3.14, 7.4.2 lmdb 驱动现在支持额外的 map_size 参数。

参见

添加备注

用户贡献的备注 6 notes

up
0
mskala at ansuz dot sooke dot bc dot ca
16 years ago
As of GDBM version 1.8.3, GDBM's underlying open call uses non-blocking calls to flock() on systems that have flock(). As a result, calls with "rd" or "wd" locking modes will return error ("Can't be reader" or "Can't be writer") instead of waiting. Use "rl" or "wl" instead, to make PHP do its own locking external to GDBM.
up
0
doppelbauer at gmail dot com
18 years ago
Windows does not support locking the database. You may use $_ENV to determine the OS:

$locking = (stripos($_ENV['OS'],'windows') === false ? 'd' : 'l');
up
0
xy ät affenkrieger.de
18 years ago
If you get some strange errors like
dba_open(): myDbFilename.db : Permission denied
than you are propably using PHP on a Windoze machine. You have to make sure that the following conditions are met:

1) Use an absolute path to your db file. Relative paths will cause problems with locking
2) Specify a locking mode - that's the second character of the mode-argument, or else opening a dba-file will cause several notices/warnings etc.

And a final, general note:
3) Always use the english PHP doc on this site - the translations are often old as hell and miss important informations

HTH, Nils.
up
-1
trohit at blue bottle dot com
17 years ago
Here's a simple example to use the dba_open function

<?php

$id
= dba_open("/tmp/test.db", "n", "gdbm");

if (!
$id) {
echo
"dba_open failed\n";
exit;
}

dba_replace("key", "This is an example!", $id);

if (
dba_exists("key", $id)) {
echo
dba_fetch("key", $id);
dba_delete("key", $id);
}

dba_close($id);
?>
up
-1
dracoirs at gmail dot com
14 years ago
Apache doesn't support Berkeley DB Btree, so you can't manipulate use db4 as the type of database if you want to do DBM authentication with Apache.

gdbm seemed to work fine though, even though it supposedly using Btree instead of hash. It makes you wonder why Apache would use hash for one dbmtype versus btree for another.

So since Apache and PHP don't have options to choose the method for the Berkeley DBs, you are out of luck.
up
-2
cbemerine at gmail dot com
15 years ago
Note the “c” create flag does not work if MySQL was built with the “cdb” DBA handler compile option which is common for many distros. By definition the cdb DBA handler is optimized for reading/writing and “no updates are allowed.”

<?php
$dbh
= dba_open( "./data2/productz", "c", "cdb") or die( "Couldn't open Database" );
?>

instead use

<?php
$dbh
= dba_open( "./data2/productz", "n", "cdb" ) or die( "Couldnt open Database" );
?>

generates this error message in the /var/log/apache2/error.log:
[Sun Sep 06 04:18:15 2009] [error] [client 192.168.1.125] PHP Warning: dba_open(./data2/productz,c) [<a href='function.dba-open'>function.dba-open</a>]: Driver initialization failed for handler: cdb: Update operations are not supported in /var/www/projects/testcdb-c.php on line 43

see user contributed comment under dba_handlers() to see which DBA handlers are supported by your build of MySQL and note about using “cdb” compiled DBA systems:

also see user contributed comment under dba_replace() about incompatibilities with cdb DBA handler compiled MySQL systems.
To Top