dBase functions
dBase
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
modifying the same dBase file will very likely 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_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.
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
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
Close a dBase database
Description
bool dbase_close
int dbase_identifier
Closes the database associated with
dbase_identifier.
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
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_replace_record
Replace a record in a dBase database
Description
bool dbase_replace_record
int dbase_identifier
array record
int dbase_record_number
Replaces the data associated with the record
record_number with the data in the
record in the database. If the number of
items in the supplied record is not equal to the number of fields
in the database, the operation will fail and false will be
returned.
dbase_record_number is an integer which
spans from 1 to the number of records in the database (as
returned by dbase_numrecords).
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
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 0, 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_get_record_with_names
Gets a record from a dBase database as an associative array
Description
array
dbase_get_record_with_names
int dbase_identifier
int record
Returns the data from record in an
associative array. The array also 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
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).
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
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.