CakeFest 2024: The Official CakePHP Conference

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
): resource|false

dba_open() は、handler を用いて mode を指定し、path にデータベースインスタンスを生成します。

パラメータ

path

通常のファイルシステムのパス。

mode

既存のデータベースへの読み込みアクセスには "r"、読込み/書き込み アクセスには "w" を指定します。 読み込み/書き込みアクセスおよび存在しない場合にデータベースの作成を するには "c" を、そして作成、削除、読込み/書き込みアクセス用には "n" を指定します。データベースは BTree モードで作られます。 その他のモード (Hash や Queue) には対応していません。

さらに、次の文字でデータベースのロック方法を指定することができます。 .lck でデータベースをロックする場合には "l"、 データベースファイル自体をロックする場合は "d" を使用してください。 アプリケーション全体で統一した方法を用いることが重要です。

アクセスのテストを行う際にロックのために待ちたくない場合、"t" を 3 番目の文字に追加することができます。明らかにデータベースのロックが 不要な場合には、"l" や "d" の代わりに "-" を使用してロックを 行わないことができます。"d"、"l" または "-" のどれも指定されない場合、 "d"が指定されたものとしてデータベースファイルをロックします。

注意:

ひとつのデータベースファイルに同時に書き込めるのは、ひとつだけです。 dba を Web サーバーで使用している際に複数のリクエストが書き込み処理を 行う必要がある場合、他の書き込みが終わってからでないと次の書き込みを 行うことができません。また、書き込み中に読み込むことはできません。 dba 拡張モジュールは、このようなことを防止するためにロックを使用します。 以下の表を参照ください:

DBA のロック処理
既にオープンしているデータベース 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: 2 番目のコールは成功します。
  • wait: 2 番目のコールは、最初のデータベースで dba_close() がコールされるまで待ちます。
  • false: 2 番目のコールは、false を返します。
  • illegal: mode パラメータで "l" および "d" を同時に使用することはできません。

handler

path にアクセスする際に使用する ハンドラの名前。 dba_open() に指定したすべてのオプションが渡され、 その機能を用いることができます。 handlernull の場合、 デフォルトのハンドラが使われます。

permission

ドライバに渡されるオプションの整数パラメータ。 chmod()permissions を同じ意味です。 デフォルトは 0644 です。

db1, db2, db3, db4, dbm, gdbm, ndbm, lmdb ドライバが permission パラメータをサポートしています。

map_size

ドライバに渡されるオプションの整数パラメータ。 OS のページサイズの倍数を指定すべきです。 ゼロを指定すると、デフォルトのマップサイズが使われます。

lmdb ドライバだけが map_size パラメータを受け入れます。

flags

データベースドライバに渡すフラグ。 null の場合、 デフォルトのフラグが渡されます。 現状、LMDB ドライバだけが DBA_LMDB_USE_SUB_DIRDBA_LMDB_NO_SUB_DIR をサポートしています。

戻り値

成功した場合に正のハンドルを返します。 失敗した場合に false を返します。

エラー / 例外

handlernull が指定されているのにデフォルトのハンドラが存在しない場合、 E_WARNING レベルのエラーが発生し、 false が返されます。

変更履歴

バージョン 説明
8.2.0 flags が追加されました。
8.2.0 handler は、nullable になりました。
7.3.14, 7.4.2 lmdb ドライバは、 map_size パラメータを追加でサポートしました。

参考

  • dba_popen() - データベースを持続的にオープンする
  • dba_close() - DBA データベースを閉じる

add a note

User Contributed Notes 6 notes

up
1
doppelbauer at gmail dot com
17 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
dracoirs at gmail dot com
13 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
0
mskala at ansuz dot sooke dot bc dot ca
15 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
xy ät affenkrieger.de
17 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
-2
trohit at blue bottle dot com
16 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
-4
cbemerine at gmail dot com
14 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