[ Team LiB ] Previous Section Next Section

Amending Elements in a Database

You can amend an entry in a database with the dba_replace() function, which requires the name of a key, the new value to add, and a valid DBA resource. It returns true if all goes well and false if an error occurs. Listing 12.2 amends the code in Listing 12.1 so that keys are added regardless of existence.

Listing 12.2 Adding or Changing Items in a Database
 1: <!DOCTYPE html PUBLIC
 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.2 Adding or Changing Items
 7:    in a database</title>
 8: </head>
 9: <body>
10: <div>
11: Adding products now...
12: <?php
13: $dbh = dba_open( "./data/products", "c", "gdbm" )
14:       or die( "Couldn't open database" );
15: dba_replace( "Sonic Screwdriver", 25.20, $dbh );
16: dba_replace( "Tricorder", 56.50, $dbh );
17: dba_replace( "ORAC AI", 2209.50, $dbh );
18: dba_replace( "HAL 2000", 4535.50, $dbh );
19: dba_close( $dbh );
20: ?>
21: </div>
22: </body>
23: </html>

We have had to change only the function calls from dba_insert() to dba_replace() to change the functionality of the script.

    [ Team LiB ] Previous Section Next Section