Home > Articles > Programming > PHP

  • Print
  • + Share This
This chapter is from the book

Amending Elements in a Database

You can amend an entry in a database with the dba_replace() function. dba_replace() 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 11.2 amends the code in Listing 11.1 so that keys are added regardless of existence.

Listing 11.2 Adding or Changing Items belonging to a Database

 1: <html>
 2: <head>
 3: <title>Listing 11.2 Adding or changing items 
 4:    belonging to database</title>
 5: </head>
 6: <body>
 7: Adding products now...
 8: <?php
 9: $dbh = dba_open( "./data/products", "c", "gdbm" ) 
10:       or die( "Couldn't open database" );
11: dba_replace( "Sonic Screwdriver", "25.20", $dbh );
12: dba_replace( "Tricorder", "56.50", $dbh );
13: dba_replace( "ORAC AI", "2209.50", $dbh );
14: dba_replace( "HAL 2000", "4535.50", $dbh );
15: dba_close( $dbh );
16: ?>
17: </body>
18: </html>

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

  • + Share This
  • 🔖 Save To Your Account