berkeley db : fetch array & display example
# DB4 FETCH
$DB4_DATABASE = "/data/TEST_DATA.DB";
$db = dba_open($DB4_DATABASE,"r","db4");
$k = dba_firstkey($db);
while($k != NULL)
$AAA = explode("\xFE",dba_fetch($k, $db));
echo "[key] $k => [value] $AAA / $AAA / $AAA / $AAA / $AAA / $AAA<br>\r\n";
$k = dba_nextkey($db);
(PHP 4, PHP 5)
dba_replace — Replace or insert entry
bool dba_replace ( string
dba_replace() replaces or inserts the entry described
value into the
database specified by
TRUE on success or
FALSE on failure.
add a note User Contributed Notes dba_replace - [4 notes]
khan666 at lycos dot co dot kr ¶
2 years ago
cbemerine at gmail dot com ¶
3 years ago
QDBM and GDBM appear to be the only DBA handlers that will allow dba_replace to work correctly. The DBA Handlers must be compiled or built into the version you are using. See dba_handlers (http://www.php.net/manual/en/function.dba-handlers.php) for more specific information:
Also when you search online you will see mention of “security” related issue, supposedly, related to the dba_replace() function. In every instance of the “security” issue documented online, the dba_open function specified the DBA handler option of “inifile”. DBA handler “inifile” is intended for the management of ini files specifically. Here are the dba_open and dba_replace code snippets related to this “security” issue:
$source=dba_open("/www/about.ini", "wlt", "inifile");
I have also seen errors listed online with DB4 and use of the dba_replace function. Though I am skeptical of those reports. Unfortunately I do not have a database with the DB4 handlers compiled in to check if there is an issue with dba_replace and the DB4 DBA handler or not. At least you are aware that there may be an issue and can check if need be.
Assuming the correct DBA handlers are “built” or compiled into your database when built for the packages you are using, there is probably not an issue with dba_replace() and actual database files. If you experience a problem with dba_replace() make sure you build in DBA handlers QDBM or GDBM.
You can not build in both QDBM and GDBM together in the same build, however you probably can build in both INIFILE and FLATFILE with either QDBM or GDBM. It is unclear if you can build in either db3 or db4 with QDBM or GDBM. It appears that DB4, CDB, INIFILE and FLATFILE are often built together.
see User contributed notes for dba_handlers() and dba_open() related to CDB.
jelte dot werkhoven at itfy dot com ¶
10 years ago
Not only will dba_replace add extra entries when trying to replace an existing smaller one, it will always add a new entry if the size of the value differs from the existing entry. This is probably due to the nature of the datum struct in DBM-type databases (I work with gdbm), which consist of a void pointer and a integer containing the size of the memory pointed to. Padding the value should solve this.
swain at panix dot com ¶
11 years ago
Note that if you replace an existing entry with a larger one, it will actually create a new entry and the old one is simply lost. This is a memory leak in other words.
You might want to try padding your values to a certain size before putting them in the db file; for example, if you are storing web pages in the db, pad them out to the next 500 byte size with spaces, and strip the spaces when you read it out. This will go a long way in saving disk space. I've seen db files grow to several megabytes with only a couple hundred text files in them.