dBase Functions
These functions allow you to access records stored in dBase-format (dbf) databases.
There is no support for indexes or memo fields. There is no support for locking, too. Two concurrent webserver processes accessing the same dBase file will ruin your database.
Unlike SQL databases, dBase "databases" cannot change the database definition afterwards. Once the file is created, the database definition is fixed. There are no indexes that speed searching or otherwise organize your data. dBase files are simple sequential files of fixed length records. Records are appended to the end of the file and delete records are kept until you call dbase_pack().
We recommend that you do not use dBase files as your production database. Choose any real SQL server instead; MySQL or Postgres are common choices with PHP. dBase support is here to allow you to import and export data to and from your web database, since the file format is commonly understood with Windows spreadsheets and organizers. Import and export of data is about all that dBase support is good for.
dBase Functions
dbase_create
Name
dbase_create — creates a dBase database
Description
int dbase_create(string filename, array fields);
The fields parameter is an array of arrays, each array describing the format of one field in the database. Each field consists of a name, a character indicating the field type, a length, and a precision.
The types of fields available are: L
Boolean. These do not have a length or precision.
M
Memo. (Note that these aren't supported by PHP.) These do not have a length or precision.
D
Date (stored as YYYYMMDD). These do not have a length or precision.
N
Number. These have both a length and a precision (the number of digits after the decimal point).
C
String.
If the database is successfully created, a dbase_identifier is returned, otherwise false is returned.
Example 1. Creating a dBase database file
// "database" name
$dbname = "/tmp/test.dbf";
// database "definition"
$def =
array(
array("date", |
"D"), |
|
---|---|---|
array("name", |
"C", |
50), |
array("age", |
"N", |
3, 0), |
array("email", |
"C", |
128), |
array("ismember", |
"L") |
);
// creation
if (!dbase_create($dbname, $def)) print "<strong>Error!</strong>";
dbase_open
Name
dbase_open — opens a dBase database
Description
int dbase_open(string filename, int flags);
The flags correspond to those for the open() system call. (Typically 0 means read-only, 1 means write- only, and 2 means read and write.)
Returns a dbase_identifier for the opened database, or false if the database couldn't be opened.
dbase_close
Name
dbase_close — close a dBase database
Description
bool dbase_close(int dbase_identifier);
Closes the database associated with dbase_identifier.
dbase_pack
Name
dbase_pack — packs a dBase database
Description
bool dbase_pack(int dbase_identifier);
Packs the specified database (permanently deleting all records marked for deletion using
dbase_delete_record.
dbase_add_record
Name
dbase_add_record — add a record to a dBase database
Description
bool dbase_add_record(int dbase_identifier, array record);
Adds the data in the record to the database. If the number of items in the supplied record isn't equal to the number of fields in the database, the operation will fail and false will be returned.
dbase_delete_record
Name
dbase_delete_record — deletes a record from a dBase database
Description
bool dbase_delete_record(int dbase_identifier, int record);
Marks record to be deleted from the database. To actually remove the record from the database, you must also call dbase_pack.
dbase_get_record
Name
dbase_get_record — gets a record from a dBase database
Description
array dbase_get_record(int dbase_identifier, int record);
Returns the data from record in an array. The array is indexed starting at 1, and includes an associative member named 'deleted' which is set to 1 if the record has been marked for deletion (see dbase_delete_record.
Each field is converted to the appropriate PHP type. (Dates are left as strings.)
dbase_numfields
Name
dbase_numfields — find out how many fields are in a dBase database
Description
int dbase_numfields(int dbase_identifier);
Returns the number of fields (columns) in the specified database. Field numbers are between 0 and dbase_numfields($db)-1, while record numbers are between 1 and dbase_numrecords($db).
Example 1. Using dbase_numfields
$rec = dbase_get_record($db, $recno);
$nf = dbase_numfields($db); for ($i=0; $i < $nf; $i++) { print $rec[$i]."<br>\n";
}
dbase_numrecords
Name
dbase_numrecords — find out how many records are in a dBase database
Description
int dbase_numrecords(int dbase_identifier);
Returns the number of records (rows) in the specified database. Record numbers are between 1 and dbase_numrecords($db), while field numbers are between 0 and dbase_numfields($db)-1.