dbase_create() creates a new dbf file in file_name using the field definitions contained in the field_defs array. If the file already exists, it will be overwritten. If the dbf file can be created, a dbf identifier is returned that can be used with the other dbf functions. FALSE is returned for all other cases.
The field_defs array is used to define the name, type, format, and number of rows in the dbf file. The structure of the array is simple. Each definition for a field is an array of two to four elements. Each of these arrays is stored in another array, in the order by which they'll be placed inside the dbf file. Sound complicated? Not really. A simple example should help clear up any misconceptions:
$simple_definition = array ( array ('An Int', 'N', 3, 0), # A field called 'An Int' that holds an integer with up to three digits array ('A String', 'C', 128), # A field called 'A String' that holds up to 128 characters arrat ('A Boolean', 'L') # A field called 'A Boolean' that holds 0 or 1 );
The outer array holds the field definitions, while the three inner arrays are definitions for a single row. The row definitions have this format:
There are arbitrary and practical limits placed on the size of dbf character and number fields. Most early implementations of the format only allowed up to 255 characters in a character field and 20 digits in a numeric field. PHP's implementation of the format doesn't have these restrictions - character fields can be moderately large (at least 32KB) - but keep in mind that these are fixed-length files.
This means that if you have a dbf file with five character fields of 32768 characters each, then every record in your file will require 160KB, regardless of how much data is put into the character fields. Use a dbf file definition like this to store a few hundred records, and you might start noticing a decrease in the space on your drive.
The dbf file format supports many other field types: floating point, memo, and in some implementations, picture fields and so on. However, PHP doesn't support any field type other than those listed in the previous table.
$db_file = '/tmp/album_catalog.dbf'; $db_structure = array ( array ('album', 'C', 64), # A field called 'album title' - holds up to 64 characters array ('artist', 'C', 64), # A field called 'artist' - holds up to 64 characters array ('year', 'N', 4, 0), # A field called 'year' - holds an integer of up to 4 digits array ('label', 'C', 64), # A field called 'record label' - holds up to 64 characters array ('loaned', 'L'), # A field called 'loaned' - holds 1 or 0 array ('last_loan', 'C', 64), # A field called 'last_loaned_to' - holds up to 64 characters array ('notes', 'C', 256) # A field called 'note' - holds up to 256 characters ); # Create database or exit with an error message $db_id = dbase_create ($db_file, $db_structure) or die ("Could not create dbf file '$db_file'\n"); # Print success message print "dbf file '$db_file' created\n";
PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing (Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch). This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/). The authors of this book have elected not to choose any options under the OPL. This online book was obtained from http://www.fooassociates.com/phpfer/ and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4 for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All persons and entities shall have neither liability nor responsibility to any person or entity with respect to any loss or damage arising from the information contained in this book.