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

search for in the

dba_close> <dbplus_xunlockrel
[edit] Last updated: Mon, 01 Nov 2010

view this page in

XXIV. dba Datenbank (dbm-style) Abstraktions-Funktionen

Diese Funktionen bilden die Basis für den Zugriff auf Datenbanken vom Typ "Berkeley DB".

"dba" implementiert eine generelle Abstraktionsschicht für verschiedene dateibasierte Datenbanken. Der Funktionsumfang ist daher beschränkt auf eine gemeinsame Teilmenge des Funktionsumfangs aktueller Datenbanken (zum Beispiel » Sleepycat Software's DB2, nicht zu verwechseln mit IBM's DB2 Software, die über die ODBC Funktionen unterstützt wird).

Das Verhalten der dba-Funktionen ist teilweise abhängig von der Implementation des zugrundeliegenden Datenbanksystems. Funktionen wie zum Beispiel dba_optimize() und dba_sync() funktionieren vielleicht bei einer Datenbank, und tun gar nichts bei einer Anderen.

Beim Aufruf von dba_open() oder dba_popen() muss einer der verfügbaren Handler-Namen aus der nachfolgenden Handler-Tabelle angegeben werden. Die tatsächlich verfügbaren Handler in Ihrer PHP-Installation können Sie mit der Funktion phpinfo() anzeigen lassen. (Um zum Zeitpunkt der PHP-Generierung die Unterstützung für einen der folgenden Handler verfügbar zu machen, fügen Sie bitte den jeweils angegebenen --with-XXXX Compilerschalter in ihren PHP-Konfigurations-Aufruf ein.)

Tabelle 43. Liste der DBA-Handler

HandlerHinweise
dbm Dbm ist der ursprüngliche und älteste Typ der "Berkeley DB" Datenbanken. Sie sollten diesen Typ wenn möglich vermeiden. Die in DB2 und gdbm enthaltenen dbm-Kompatibilitätsfunktionen werden ebenfalls nicht unterstützt, da sie nur auf Quellcode-Basis kompatibel sind, aber Dateien im originalen dbm-Format nicht bearbeiten können. (--with-dbm)
ndbm Ndbm ist neuer und flexibler als Dbm. Es hat jedoch die meisten Einschränkungen von Dbm übernommen und ist daher ebenfalls nicht empfehlenswert. (--with-ndbm)
gdbm Gdbm ist der » GNU Datenbank Manager. (--with-gdbm)
db2 DB2 ist » Sleepycat Software's DB2. Es wird beschrieben als "Werkzeugsatz für eingebaute Unterstützung von Hochleistungs-Datenbanken für Standalone- und Client/Server- Applikationen". (--with-db2)
db3 DB3 ist » Sleepycat Software's DB3. (--with-db3)
cdb Cdb ist "ein schnelles, zuverlässiges und kompaktes Paket zum Erstellen und Auslesen konstanter Datenbanken." Es stammt vom Autor von qmail und kann » hier heruntergeladen werden. Da es für konstante Daten ausgelegt ist, werden nur Lese-Operationen unterstützt. (--with-cdb)

Beispiel 390. DBA Beispiel

<?php

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

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

dba_replace ("key", "Dies ist ein Beispiel!", $id);

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

dba_close ($id);
?>

DBA arbeitet binärsicher und besitzt keine willkürlichen Limits. Es erbt allerdings alle Einschränkungen von der zugrundeliegenden Datenbank-Implementation.

Alle dateibasierten Datenbanken müssen einen Weg zur Verfügung stellen, um den Dateimodus einer neu erstellten Datenbank zu bestimmen, soweit dies überhaupt möglich ist. Der Dateimodus wird gewöhnlich als viertes Argument an die Funktionen dba_open() oder dba_popen() übergeben.

Sie können auf alle Einträge in einer Datenbank sequentiell durch die Verwendung von dba_firstkey() und dba_nextkey() zugreifen. Während die Datenbank so durchlaufen wird, darf sie nicht verändert werden.

Beispiel 391. Durchlaufen einer Datenbank

<?php

# ...Oeffnen der Datenbank...

$key = dba_firstkey ($id);

while (
$key != false) {
    if (...) {
# den Schluessel fuer spaetere Aktionen merken
       
$handle_later[] = $key;
    }
   
$key = dba_nextkey ($id);
}

for (
$i = 0; $i < count($handle_later); $i++)
   
dba_delete ($handle_later[$i], $id);

?>

Inhaltsverzeichnis

dba_close — Datenbank schließen
dba_delete — Löschen des zu key gehörigen Eintrages
dba_exists — Überprüfen, ob Datensatz mit Schlüssel key existiert
dba_fetch — Datensatz für Schlüssel "key" auslesen
dba_firstkey — Ersten Schlüssel bestimmen
dba_handlers — List all the handlers available
dba_insert — Datensatz einfügen
dba_key_split — Splits a key in string representation into array representation
dba_list — List all open database files
dba_nextkey — Nachfolgenden Schlüssel bestimmen
dba_open — Verbindung zu einer Datenbank öffnen
dba_optimize — Optimieren einer Datenbank
dba_popen — Persistente Datenbank-Verbindung öffnen
dba_replace — Datensatz ersetzen oder einfügen
dba_sync — Datenbank synchronisieren


dba_close> <dbplus_xunlockrel
[edit] Last updated: Mon, 01 Nov 2010
 
add a note add a note User Contributed Notes dba Datenbank (dbm-style) Abstraktions-Funktionen
Franz Korntner 21-Feb-2012 09:10
If you need a 'download data' button that automatically fires up a spreadsheet (like Excel), find that fputcsv() isn't working as expected, that none of the installed DBA database engines create a spreadsheet that can be opened, and that XLS generating components are just too heavy weight, then this might just hit the spot:

<?php
// simple table to present
$data = array(
    array(
'col1','col2'),
    array(
1,2),
    array(
3,4)
);

// pretend content (which is XML) is XLS native
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=\"sheet.xls\";" );

// construct skeleton
$dom = new DOMDocument('1.0', 'utf-8');
$dom->formatOutput = $dom->preserveSpaces = true; // optional
$n = new DOMProcessingInstruction('mso-application', 'progid="Excel.Sheet"');
$dom->appendChild($n);

$workbook = $dom->appendChild(new DOMElement('Workbook'));
$workbook->setAttribute('xmlns','urn:schemas-microsoft-com:office:spreadsheet');
$workbook->setAttribute('xmlns:o','urn:schemas-microsoft-com:office:office');
$workbook->setAttribute('xmlns:x','urn:schemas-microsoft-com:office:excel');
$workbook->setAttribute('xmlns:ss','xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet');
$workbook->setAttribute('xmlns:html','http://www.w3.org/TR/REC-html40');

$styles = $workbook->appendChild(new DOMElement('Styles'));
$style = $styles->appendChild(new DOMElement('Style'));
$style->setAttribute('ss:ID','Default');
$worksheet = $workbook->appendChild(new DOMElement('Worksheet'));
$worksheet->setAttribute('ss:Name','sheet1');
$xmltable = $worksheet->appendChild(new DOMElement('Table'));

// populate with data
foreach ($data as $datarow) {
   
$xmlrow = $xmltable->appendChild(new DOMElement('Row'));
    foreach (
$datarow as $datacell) {
       
$xmlcell = $xmlrow->appendChild(new DOMElement('Cell'));
       
$xmldata = $xmlcell->appendChild(new DOMElement('Data', $datacell));
       
$xmldata->setAttribute('ss:Type', is_numeric($datacell) ? 'Number' : 'String');
    }
}

// display and quit
echo $dom->saveXML();
?>
doppelbauer at gmail dot com 14-Oct-2006 07:47
jason 21-Dec-2004 07:33
Don't make the dumb mistake I did, if you copy the first example at the top and modify it for test, the dba_open specifies "n" which will truncate the database that you are pointing to. Make sure you change it to "r" if you just want to read from an existing dbm file.
kevinphpdotnet at stormtide dot ca 17-Mar-2004 11:10
When using db4 on redhat 7.3 you may get signal 11s on the apache child processes. The installation test scripts will report that db4 is working correctly as the cli will not sig 11 out. The solution is to check to see if mod_rewrite is installed with apache, if it is either dereference it from libdb.so.3 or build apache without mod rewrite. Once this is done you will no longer have your child processes dying out and db4 will work. If you do not get a sig 11 after dba_open just ignore this comment.
djm at web dot us dot uu dot net 17-Oct-1999 03:28
With db2, you need to call dba_sync() to get the data written to disk; the examples are missing this.  db2 uses
the BTREE file format, not the more common HASH.
BTREE is faster, though, in my tests, so it's a good
choice.

 
show source | credits | sitemap | contact | advertising | mirror sites