# dBase - PHP

The following functions work on dBase files, which typically end with a .dbf extension. If you are using the precompiled version for Windows, you will need to load the dBase extension by editing php.ini or using the dl function. The extension is likely called php_dbase.dll but was unavailable at the time of this writing. On other operating systems it's easy to compile dBase support into PHP. The dBase functionality in PHP is somewhat limited. Index and memo fields are not supported. Neither is any kind of locking. The dBase functionality in PHP is meant to be a means of importing data from what has become somewhat of a lowest common denominator in data exchange. Jim Winstead added dBase support to PHP.

The dbase_add_record function adds a record to the database specified by a database identifier returned by dbase_open. The record array contains an element for each field in the database, in order and starting at zero. If the correct number of fields is not supplied, FALSE is returned.

<?
//open connection to database
$db = dbase_open("customer.dbf", 2); //create record to be added$newRecord = array("John Smith", 100.00, "19980901","Y");
dbase_add_record($db,$newRecord);
//close connection
dbase_close($db); ?> boolean dbase_close(integer database) The dbase_close function closes a database. See other functions in this section for examples of use. integer dbase_create(string filename, array fields) The dbase_create function creates a dBase database. The fields argument is an array of arrays that describe fields. Each array may have up to four elements. In order, they are name, type, length, and precision. Type is a single character. Some types require length, and precision; others do not. If a database is successfully created, a database identifier is returned; otherwise FALSE is returned. <? // create field definition$fields = array(
array("Name", "C", 32),
array("Balance", "N", 8, 2),
array("Birthday", "D"),
array("Commercial", L));
$db = dbase_create("customer.dbf",$fields);
dbase_close($db); ?> boolean dbase_delete_record(integer database, integer record) The dbase_delete_record function marks a record for deletion. The record will remain in the database until dbase_pack is called. <? //open connection to database$db = dbase_open("customer.dbf", 2);
//mark record for deletion
dbase_delete_record($db, 2); //close connection dbase_close($db);
?>

array dbase_get_record(integer database, integer record)
The dbase_get_record function returns the fields of a record in an array. The first field will be numbered zero. In addition, an element indexed by deleted will contain 1 if the row was marked for deletion. Records are numbered from one.

<?
//connect to database
$db = dbase_open("customer.dbf", 2); //get some information about database$numRecords = dbase_numrecords($db);$numFields = dbase_numfields($db); // get every record for($index = 1; $index =$numRecords; $index++) { //get a record$record = dbase_get_record($db,$index);
print("H3>Record $index/H3>n"); //loop over fields for($index2 = 0; $index2$numFields;
$index2++) { print("B>Field$index2:/B>");
print($record[$index2]);
print("<BR>n");
}
//print deletion status
print("B>Deleted:/B> ");
print($record["deleted"]); print("<BR>n"); } //close connection dbase_close($db);
?>

array dbase_get_record_with_names(integer database, integer record)
This function behaves like dbase_get_record, except that instead of being indexed by integers, fields are indexed by their names.

<?
//connect to database
$db = dbase_open("customer.dbf", 2); // get every record for($index = 1; $index = dbase_numrecords($db);
$index++) {$record = dbase_get_record_with_names($db,$index);
print("H3>Record $index/H3>n"); //loop over fields while(list($key, $value) = each($record))
{
print("B>Field $key:$value/B><BR>n");
}
}
//close connection
dbase_close($db); ?> integer dbase_numfields(integer database) The dbase_numfields function returns the number of fields for the given database. See the description of dbase_get_record for an example of its use. integer dbase_numrecords(integer database) The dbase_numrecords function returns the number of records in the database. See the description of dbase_get_record for an example of its use. integer dbase_open(string filename, integer mode) Use dbase_open to get a dbase identifier. This integer is needed for identifying which database to operate on. The mode may be 0 for read-only, 1 for write-only, or 2 for allowing both reading and writing. FALSE is returned if the database cannot be opened. The other examples in this section demonstrate dbase_open. boolean dbase_pack(integer database) When rows are deleted in a dbase database, they are simply marked for deletion. Use the base_pack function to permanently remove these rows, thus packing the database. <? //connect to database$db = dbase_open("customer.dbf", 2);
//removed rows marked for deletion
dbase_pack($db); //close connection dbase_close($db);
?>

boolean dbase_replace_record(integer database, array record, integer record_number)
Use dbase_replace_record to change the contents of a record. The record argument must have one element for each field defined in the database. Record numbers start counting at one.

<?
$db = dbase_open("customer.dbf", 2);$Record = array("John Smith", 200.00, "19990901","Y");
dbase_replace_record($db,$Record, 1);
dbase_close(\$db);
?>