downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

dba_optimize> <dba_nextkey
Last updated: Fri, 13 Nov 2009

view this page in

dba_open

(PHP 4, PHP 5)

dba_openデータベースをオープンする

説明

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

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

パラメータ

path

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

mode

既存のデータベースへの読み込みアクセスには "r"、読込み/書き込み アクセスには "w" を指定します。 読み込み/書き込みアクセスおよび存在しない場合にデータベースの作成を するには "c" を、そして作成、削除、読込み/書き込みアクセス用には "n" を指定します。

さらに、次の文字でデータベースのロック方法を指定することができます。 .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() に指定したすべてのオプションが 渡され、その機能を用いることができます。

返り値

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

変更履歴

バージョン 説明
4.3.0 ネットワーク接続されたデータベースファイルをオープンすることができます。 しかし、(http や ftp のような)ソケット接続が使用された場合、 リソース自体ではなくこの接続がロックされます。このような場合、 このリソースに関してのロック処理は単に無視されることになり、 他の解決策を見付ける必要があることに留意する必要があります。
4.3.0 ロック処理と mode 修正子 "l", "d", "-", "t" が追加されました。 以前のバージョンの PHP では、GDBM 以外のデータベースハンドラで 同時データベースアクセスに対する保護を行うためには、セマフォを 使用する必要がありました。 System V セマフォサポート を参照ください。
4.3.5 以前 オープンモード 'c' はいくつかの内部ハンドラでは正常に動作せず、 既存のデータベースにデータを追加するのではなく、データベースを 切り捨ててしまっていました。また、dbm と ndbm は典型的な設定で モード 'c' の処理が正常に動作しません(これは修正できません)。

参考

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



dba_optimize> <dba_nextkey
Last updated: Fri, 13 Nov 2009
 
add a note add a note User Contributed Notes
dba_open
cbemerine at gmail dot com
06-Sep-2009 08:59
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.
mskala at ansuz dot sooke dot bc dot ca
17-May-2008 06:15
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.
trohit at blue bottle dot com
23-Nov-2007 08:34
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);
?>
doppelbauer at gmail dot com
20-Oct-2006 11:36
Windows does not support locking the database. You may use $_ENV to determine the OS:

$locking = (stripos($_ENV['OS'],'windows') === false ? 'd' : 'l');
xy ät affenkrieger.de
30-Mar-2006 11:13
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.

dba_optimize> <dba_nextkey
Last updated: Fri, 13 Nov 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites