Team LiB
Previous Section Next Section

Reading Data

Entering data into the database is fairly easy; you just provide keys and values. Both have to be strings; however, you can use serialize() and unserialize() to enter other kinds of data into the database. Reading values from the database is a bit trickier and not as easy as it would be with a database or a "real" associative array, but is still doable with relatively little effort.

When you know the exact name of a key, dba_fetch() returns the associated value. In the preceding setup, the following code would print out "Coggeshall":

echo dba_fetch("John", $dba);

However, in most cases, you are interested in traversing all data. In this case, you have to imagine working with PHP's dba functions as with working with a resultset from a "real" database. You start at the first entry and then move forward, step by step.

From PHP's dba functions, the following ones are useful in this task:

  • dba_firstkey() returns the name of the key of the first entry in the database.

  • dba_nextkey() returns the name of the key of the next entry in the database.

Because dba_nextkey() return false when no next key/entry is available, a simple while loop prints out all data in the dba file. Listing 26.6 does so for all entries in the book contributor's list (this list is not complete; it even excludes the author of these lines, but it's just demoware). Note that we set the access mode to r because this time we are reading, not writing.

Listing 26.6. Reading Data from the dba File
  require_once "";
  $dba = @dba_open("dba.db", "r", $dbahandler);
  if (!$dba) {
    echo "Failed opening database.";
  } else {
    echo "<ul>";
    if ($key = dba_firstkey($dba)) {
      do {
        printf("<li>%s %s</li>",
               htmlspecialchars(dba_fetch($key, $dba)));
      } while ($key = dba_nextkey($dba));
    } else {
      echo "Error reading from database (or no entries available).";

Figure 26.2 shows the output of Listing 26.6 using the sample data entered into the database using Listings 26.126.5.

Figure 26.2. All data in the database.

If you are curious, this is how the file now looks (remember, we used flatfile):


A space character at the beginning of a line hints that this entry is deleted (but still consumes space in the database file).

And that's basically all you need to know about PHP's dba functions. There are, however, other dba functions worth mentioning:

  • dba_optimize() tries to compress dba files by removing "holes" created by deleting entries (deleting from dba files often deletes only the information but does not regenerates the space). This works only by handlers that support this; with others, this function is without effect.

  • dba_sync() forces the dba handler to write all changes to the database to the hard disk. For instance, the db2 handler requires this for changes to become effective. For handlers that do not support synchronization, this function is without effect.

    Team LiB
    Previous Section Next Section