First:
Make a firstdbm.db file.
Set attribute Read and Write.
--------------------------------------
<html><body>
<?
$dbm = dbmopen("firstdbm","w");
echo "<b>\$dbm: $dbm = dbmopen(\"firstdbm\", \"w\");</b>< br>";
echo "<i>".dblist()." = dblist();</i>< br>< br>";
$fruit = "apple";
$colour = "red";
if (dbmexists ($dbm, $fruit)) {
$colour = dbmfetch ($dbm, $fruit);
echo "\$colour: $colour = dbmfetch(\$dbm: $dbm, \$fruit: $fruit);< br>";
$key = dbmreplace($dbm, $fruit, "green");
echo "\$key: $key = dbmreplace(\$dbm: $dbm, \$fruit: $fruit, \"green\");< br>";
$colour = dbmfetch ($dbm, $fruit);
echo "\$colour: $colour = dbmfetch(\$dbm: $dbm, \$fruit: $fruit);< br>";
if ( !dbmdelete($dbm, $fruit) ) {
echo "False (OK) = dbmdelete(\$dbm: $dbm, \$fruit: $fruit);< br>";
} else {
echo "True (NOT OK) = dbmdelete(\$dbm: $dbm, \$fruit: $fruit);< br>";
}
$colour = dbmfetch ($dbm, $fruit);
echo "\$color: $colour = dbmfetch(\$dbm: $dbm, \$fruit: $fruit);< br><hr>";
$i = 1;
$key = dbmfirstkey($dbm);
while ($key) {
if ($i==1) {
echo "\$key: $key = dbmfirstkey(\$dbm: $dbm);< br>";
} else {
echo "\$key: $key = dbmnextkey(\$dbm: $dbm);< br>";
}
$i++;
$colour = dbmfetch ($dbm, $key);
echo "\$colour: $colour = dbmfetch(\$dbm: $dbm, \$key: $key);< br>";
$key = dbmnextkey($dbm,$key);
}
} else {
dbminsert ($dbm, $fruit, $colour);
echo "dbminsert(\$dbm: $dbm, \$fruit: $fruit, \$colour: $colour);< br>";
dbminsert ($dbm, "pear", "yellow");
echo "dbminsert(\$dbm: $dbm, \"pear\", \"yellow\");< br>";
dbminsert ($dbm, "apricot", "pink");
echo "dbminsert(\$dbm: $dbm, \"apricot\", \"pink\");< br><hr>";
$i = 1;
$key = dbmfirstkey($dbm);
while ($key) {
if ($i==1) {
echo "\$key: $key = dbmfirstkey(\$dbm: $dbm);< br>";
} else {
echo "\$key: $key = dbmnextkey(\$dbm: $dbm);< br>";
}
$i++;
$colour = dbmfetch ($dbm, $key);
echo "\$colour: $colour = dbmfetch(\$dbm: $dbm, \$key: $key);< br>";
$key = dbmnextkey($dbm,$key);
}
}
echo "< br>";
if (dbmclose ($dbm)) {
echo "<b>True (OK) = dbmclose(\$dbm: $dbm);</b>< br>";
} else {
echo "<b>False (NOT OK) = dbmclose(\$dbm: $dbm);</b>< br>";
};
?>
</body></html>
--------------------------------------
The first run output:
$dbm: 1 = dbmopen("firstdbm", "w");
ndbm support enabled = dblist();
dbminsert($dbm: 1, $fruit: apple, $colour: red);
dbminsert($dbm: 1, "pear", "yellow");
dbminsert($dbm: 1, "apricot", "pink");
$key: pear = dbmfirstkey($dbm: 1);
$colour: yellow = dbmfetch($dbm: 1, $key: pear);
$key: apricot = dbmnextkey($dbm: 1);
$colour: pink = dbmfetch($dbm: 1, $key: apricot);
$key: apple = dbmnextkey($dbm: 1);
$colour: red = dbmfetch($dbm: 1, $key: apple);
True (OK) = dbmclose($dbm: 1);
--------------------------------------
The second run output:
$dbm: 1 = dbmopen("firstdbm", "w");
ndbm support enabled = dblist();
$colour: red = dbmfetch($dbm: 1, $fruit: apple);
$key: 0 = dbmreplace($dbm: 1, $fruit: apple, "green");
$colour: green = dbmfetch($dbm: 1, $fruit: apple);
False (OK) = dbmdelete($dbm: 1, $fruit: apple);
$color: = dbmfetch($dbm: 1, $fruit: apple);
$key: pear = dbmfirstkey($dbm: 1);
$colour: yellow = dbmfetch($dbm: 1, $key: pear);
$key: apricot = dbmnextkey($dbm: 1);
$colour: pink = dbmfetch($dbm: 1, $key: apricot);
True (OK) = dbmclose($dbm: 1);
XXVI. DBM 関数 [非推奨]
導入
これらの関数により、レコードを dbm 形式 のデータベースに 格納できるようになります。この形式のデータベース (組み込みの フラットファイルライブラリと同様に、Berkeley DB・» GDBM・その他のシステムライブラリにより サポートされています) は、(リレーショナルデータベースにより サポートされる事細かなレコード形式ではなく)キーと値の組み合わせを 格納します。
注意: dbm サポートは古くなっており、 (dbm形式の)データベース抽象化レイヤ関数 を代わりに使用することが推奨されています。
注意: この拡張モジュールは PHP から削除されました。PHP 5.0.0 以降のバージョンには存在しません。
要件
この関数を使用するには、使用するデータベースのサポートを指定して PHP をコンパイルする必要があります。サポートされるデータベースの 一覧を参照ください。
インストール手順
以下の関数を使用するには、--with-db オプションを指定し、dbm サポートを 有効にして PHP をコンパイルする必要があります。 さらに、使用するデータベースのサポー トを確認する必要があります。また、いくつかのシステムライブラリを 使用することが可能です。
実行時設定
設定ディレクティブは定義されていません。
リソース型
関数 dbmopen() は、データベース ID を返します。 この ID は、他の dbm 関数で使用されます。
定義済み定数
定数は定義されていません。
例
例 471. DBM の例
<?php
$dbm = dbmopen("lastseen", "w");
if (dbmexists($dbm, $userid)) {
$last_seen = dbmfetch($dbm, $userid);
} else {
dbminsert($dbm, $userid, time());
}
do_stuff();
dbmreplace($dbm, $userid, time());
dbmclose($dbm);
?>
目次
- dblist — 使用されている DBM 互換ライブラリの一覧を返す
- dbmclose — dbm データベースを閉じる
- dbmdelete — DBM データベースから値を削除する
- dbmexists — DBM データベース中に、キーに対応する値があるかどうかを調べる
- dbmfetch — DBM データベースからキーの値を取り出す
- dbmfirstkey — DBM データベースから最初のキーを取り出す
- dbminsert — DBM データベースへ値を挿入する
- dbmnextkey
- dbmopen — DBM データベースをオープンする
- dbmreplace — DBM データベース中の値を置き換える
DBM 関数 [非推奨]
01-Mar-2002 03:16
06-Oct-2001 02:27
NOTE: DBM and DBA are two different extensions which must
be configured separately.
Hi *,
names of these functions was changed in PHP4.
Refer to http://www.faqts.com/knowledge_base/view.phtml/aid/1988
or http://www.zend.com/manual/ref.dba.php .
Regards Jindra
07-Jul-2001 10:23
Be advised that almost every function in this section either has no documentation or incorrectly documented regarding return codes. If you want to use the DB methods safely you will have to reverse engineer the return codes and hope that they do not change values returned by the functions in the future to match the incorrect documentation.
In my experience, the return code of 0 or FALSE indicates no error. This matches the behavior of GDBM which I am using as my underlying DB manager. This may not be true for people using another manager, or the builtin flatfile code.