[ Team LiB ] Previous Section Next Section

Reading from a Database

Now that you can add data to your database, you need to find a way to fetch it. You can extract an individual element from the database with the dba_fetch() function. dba_fetch() requires the name of the element you want to access and a valid DBA resource, and it returns the value you are accessing as a string. So, to access the price of the "Tricorder" item, you would use the following code:

$price = dba_fetch( "Tricorder", $dbh );

If the "Tricorder" element does not exist in the database, dba_fetch() returns false.

You won't always know the names of all the keys in the database, however. What would you do if you needed to output every product and price to the browser without hard-coding the product names into your script? PHP provides a mechanism by which you can loop through every element in a database.

You can get the first key in a database with the dba_firstkey() function, which requires a DBA resource and returns the first key. This won't necessarily be the first element you added because DBM-like databases often maintain their own ordering systems. After you've retrieved the first key, you can access each subsequent key with the dba_nextkey() function. dba_nextkey() also requires a DBA resource and returns an element's key. By combining these functions with dba_fetch(), you can now list an entire database.

Listing 12.3 outputs the products database to the browser. We acquire the first key in the database on line 24 using the dba_firstkey() function. We then use a while loop on line 25 to work our way through all the elements in the database. Elements are acquired with the call to dba_fetch() on line 26. After we have written the element to the browser, we use dba_nextkey() on line 29 to acquire the next key and assign it to the $key variable. When there are no more keys to acquire, dba_nextkey() returns false and the test expression on line 25 halts the loop.

Listing 12.3 Reading All Records from a Database
 2:   "-//W3C//DTD XHTML 1.0 Strict//EN"
 3:   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 4: <html>
 5: <head>
 6: <title>Listing 12.3 Reading All
 7:    Records from  a Database </title>
 8: </head>
 9: <body>
10: <div>
11: <p>
12:  Here at the Impossible Gadget Shop
13:  we're offering the following exciting
14:  products:
15: </p>
16: <table border="1" cellpadding ="5">
17: <tr>
18: <td align="center"> <b>product</b></td>
19: <td align="center"> <b>price</b> </td>
20: </tr>
21: <?php
22: $dbh = dba_open( "./data/products", "c", "gdbm" )
23:       or die( "Couldn't open database" );
24: $key = dba_firstkey( $dbh );
25: while ( $key != false ) {
26:   $value = dba_fetch( $key, $dbh);
27:   print "<tr><td align = \"left\"> $key </td>";
28:   print "<td align = \"right\"> \$".sprintf( "%01.2f", $value )."</td></tr>";
29:   $key = dba_nextkey( $dbh );
30: }
31: dba_close( $dbh );
32: ?>
33: </table>
34: </div>
35: </body>
36: </html>

Figure 12.1 shows the output from Listing 12.3.

Figure 12.1. Reading all records from a database.


    [ Team LiB ] Previous Section Next Section