Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : dBase Functions

dBase Functions

Introduction

These functions allow you to access records stored in dBase-format (dbf) databases.

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().

The types of dBase fields available are:

Table50.Available types of fields

Field dBase Type Format Additional information
M Memo n/a This type is not supported by PHP, such field will be ignored
D Date YYYYMMDD The field length is limited to 8
N Number A number You must declare a length and a precision (the number of digits after the decimal point)
C String A string You must declare a length. When retrieving data, the string will be right-padded with spaces to fit the declared length.
L Boolean T or Y for TRUE, F or N for FALSE Stored and returned as an integer (1 or 0)
F Float A float number Support for this type of field was added in PHP 5.2.0


Warning:

There is no support for indexes or memo fields. There is no support for locking, too. Two concurrent web server processes modifying the same dBase file will very likely ruin your database.

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, because the file format is commonly understood by Windows spreadsheets and organizers.

Installation

In order to enable the bundled dbase library and to use these functions, you must compile PHP with the --enable-dbase option.

Runtime Configuration

This extension has no configuration directives defined in php.ini.

Resource Types

This extension has no resource types defined.

Examples

Many examples in this reference require a dBase database. We will use /tmp/test.dbf that will be created in the example of dbase_create().

Predefined Constants

This extension has no constants defined.

Table of Contents

dbase_add_record — Adds a record to a database
dbase_close — Closes a database
dbase_create — Creates a database
dbase_delete_record — Deletes a record from a database
dbase_get_header_info — Gets the header info of a database
dbase_get_record_with_names — Gets a record from a database as an associative array
dbase_get_record — Gets a record from a database as an indexed array
dbase_numfields — Gets the number of fields of a database
dbase_numrecords — Gets the number of records in a database
dbase_open — Opens a database
dbase_pack — Packs a database
dbase_replace_record — Replaces a record in a database

Code Examples / Notes » ref.dbase

hadi rusiah / deegos

If you are using PHP < 5, you can use this function to retrieve dbf header
<?
function get_dbf_header($dbfname) {
  $fdbf = fopen($dbfname,'r');
  $dbfhdrarr = array();
  $buff32 = array();
  $i = 1;
  $goon = true;
  while ($goon) {
     if (!feof($fdbf)) {
        $buff32 = fread($fdbf,32);
        if ($i > 1) {
           if (substr($buff32,0,1) == chr(13)) {
              $goon = false;
           } else {
              $pos = strpos(substr($buff32,0,10),chr(0));
              $pos = ($pos == 0?10:$pos);
              $fieldname = substr($buff32,0,$pos);
              $fieldtype = substr($buff32,11,1);
              $fieldlen = ord(substr($buff32,16,1));
              $fielddec = ord(substr($buff32,17,1));
array_push($dbfhdrarr, array($fieldname,$fieldtype,$fieldlen,$fielddec));
           }
        }
        $i++;
     } else {
        $goon = false;
     }
  }
  fclose($fdbf);
  return($dbfhdrarr);
}
$arr = get_dbf_header('/data/file.dbf');
print_r($arr);
?>


bi.idan

I know lots of you dosent really use dbase, but i've builded a class to help the one how dose.
(sorry for bad english)
- dbase.php
<?php
set_time_limit (0);
// site_path defined by parent
require_once (SITE_PATH. '/server/php/libs/dbase/handler.php');
/* DBase (dbf)
* manage dbf files, exports and search functionality
* with buildin optimizers for fast performance
*/
class DBase
{
private $handler = false;
private $searchopt = array (); // Search optimizer

private function unload ()
{
if ($this-> handler !== false)
unset ($this-> handler);
}

public function __construct ($file = false)
{
if ($file !== false)
$this-> load ($file);
}

public function __destruct ()
{
$this-> unload ();
}

public function load ($file)
{
$resource = dbase_open ($file, 0);
$this-> handler = new DBase_Handler ($resource);

return $this-> handler;
}

/* Search
* search for string inside header
* returns record number
* false returned if not found or error occurred
* limit_results gets int or false, limit_results equels one will limit the
* search results for one result only, false for no limit
*/
public function search ($headerText, $string, $limit_results = false, $handler = false)
{
if ($handler === false)
$handler = $this-> handler;

if ($this-> searchopt [$headerText][$string])
return $this-> searchopt [$headerText][$string];
else
{
$size = $handler-> getSize ();
if ( ( $headerNumber = $handler-> getHeaderNumber ($headerText) ) !== false)
{
$results = array ();
for ($i = 1; $i < $size; $i++)
{
$record = $handler-> getRecord ($i, false); // Disabled optimizer to prevent memory overflow
if (trim ($record [$headerNumber]) == $string)
{
$results[] = $i;

if ( ($limit_results !== false) && (sizeof ($results) == $limit_results) )
break;
}
}

if (sizeof ($results) > 0)
{
$this-> searchopt [$headerText][$string] = $results;
return $this-> search ($headerText, $string, $handler);
}

return false;
} else
return false;
}
}
}
?>
- dbase_handler.php
<?php
/* DBase Handler (dbf)
* handles dbase resource
*/
class DBase_Handler
{
private $resource;
private $size; // Records Count
private $header = array ();
private $dataopt = array (); // Data optimizer

private function setHeader ()
{
$this-> header = dbase_get_header_info ($this-> resource);
}

public function __construct ($resource)
{
$this-> resource = $resource;
$this-> setHeader ();
$this-> size = dbase_numrecords ($this-> resource);
}

public function __destruct ()
{
dbase_close ($this-> resource);
}

public function getRecord ($record_number, $dataopt = true)
{
if ($record_number > $this-> size)
return false;
else
{
if ($this-> dataopt [$record_number])
return $this-> dataopt [$record_number];
else
{
$record = dbase_get_record ($this-> resource, $record_number);
if ($dataopt === true) // Data saving optimizer
{
$this-> dataopt [$record_number] = $record;
return $this-> getRecord ($record_number);
} else
return $record;
}
}
}

public function getHeaderNumber ($headerText)
{
foreach ($this-> header as $index => $header)
{
if ($header ['name'] == $headerText)
{
return $index;
break;
}
}

return false;
}

public function getHeader ($headerNumber)
{
if ($headerNumber <= sizeof ($this-> header))
return $this-> header [$headerNumber];
else
return false;
}

public function getSize ()
{
return $this-> size;
}
}
?>


Change Language


Follow Navioo On Twitter
.NET Functions
Apache-specific Functions
Alternative PHP Cache
Advanced PHP debugger
Array Functions
Aspell functions [deprecated]
BBCode Functions
BCMath Arbitrary Precision Mathematics Functions
PHP bytecode Compiler
Bzip2 Compression Functions
Calendar Functions
CCVS API Functions [deprecated]
Class/Object Functions
Classkit Functions
ClibPDF Functions [deprecated]
COM and .Net (Windows)
Crack Functions
Character Type Functions
CURL
Cybercash Payment Functions
Credit Mutuel CyberMUT functions
Cyrus IMAP administration Functions
Date and Time Functions
DB++ Functions
Database (dbm-style) Abstraction Layer Functions
dBase Functions
DBM Functions [deprecated]
dbx Functions
Direct IO Functions
Directory Functions
DOM Functions
DOM XML Functions
enchant Functions
Error Handling and Logging Functions
Exif Functions
Expect Functions
File Alteration Monitor Functions
Forms Data Format Functions
Fileinfo Functions
filePro Functions
Filesystem Functions
Filter Functions
Firebird/InterBase Functions
Firebird/Interbase Functions (PDO_FIREBIRD)
FriBiDi Functions
FrontBase Functions
FTP Functions
Function Handling Functions
GeoIP Functions
Gettext Functions
GMP Functions
gnupg Functions
Net_Gopher
Haru PDF Functions
hash Functions
HTTP
Hyperwave Functions
Hyperwave API Functions
i18n Functions
IBM Functions (PDO_IBM)
IBM DB2
iconv Functions
ID3 Functions
IIS Administration Functions
Image Functions
Imagick Image Library
IMAP
Informix Functions
Informix Functions (PDO_INFORMIX)
Ingres II Functions
IRC Gateway Functions
PHP / Java Integration
JSON Functions
KADM5
LDAP Functions
libxml Functions
Lotus Notes Functions
LZF Functions
Mail Functions
Mailparse Functions
Mathematical Functions
MaxDB PHP Extension
MCAL Functions
Mcrypt Encryption Functions
MCVE (Monetra) Payment Functions
Memcache Functions
Mhash Functions
Mimetype Functions
Ming functions for Flash
Miscellaneous Functions
mnoGoSearch Functions
Microsoft SQL Server Functions
Microsoft SQL Server and Sybase Functions (PDO_DBLIB)
Mohawk Software Session Handler Functions
mSQL Functions
Multibyte String Functions
muscat Functions
MySQL Functions
MySQL Functions (PDO_MYSQL)
MySQL Improved Extension
Ncurses Terminal Screen Control Functions
Network Functions
Newt Functions
NSAPI-specific Functions
Object Aggregation/Composition Functions
Object property and method call overloading
Oracle Functions
ODBC Functions (Unified)
ODBC and DB2 Functions (PDO_ODBC)
oggvorbis
OpenAL Audio Bindings
OpenSSL Functions
Oracle Functions [deprecated]
Oracle Functions (PDO_OCI)
Output Control Functions
Ovrimos SQL Functions
Paradox File Access
Parsekit Functions
Process Control Functions
Regular Expression Functions (Perl-Compatible)
PDF Functions
PDO Functions
Phar archive stream and classes
PHP Options&Information
POSIX Functions
Regular Expression Functions (POSIX Extended)
PostgreSQL Functions
PostgreSQL Functions (PDO_PGSQL)
Printer Functions
Program Execution Functions
PostScript document creation
Pspell Functions
qtdom Functions
Radius
Rar Functions
GNU Readline
GNU Recode Functions
RPM Header Reading Functions
runkit Functions
SAM - Simple Asynchronous Messaging
Satellite CORBA client extension [deprecated]
SCA Functions
SDO Functions
SDO XML Data Access Service Functions
SDO Relational Data Access Service Functions
Semaphore
SESAM Database Functions
PostgreSQL Session Save Handler
Session Handling Functions
Shared Memory Functions
SimpleXML functions
SNMP Functions
SOAP Functions
Socket Functions
Standard PHP Library (SPL) Functions
SQLite Functions
SQLite Functions (PDO_SQLITE)
Secure Shell2 Functions
Statistics Functions
Stream Functions
String Functions
Subversion Functions
Shockwave Flash Functions
Swish Functions
Sybase Functions
TCP Wrappers Functions
Tidy Functions
Tokenizer Functions
Unicode Functions
URL Functions
Variable Handling Functions
Verisign Payflow Pro Functions
vpopmail Functions
W32api Functions
WDDX Functions
win32ps Functions
win32service Functions
xattr Functions
xdiff Functions
XML Parser Functions
XML-RPC Functions
XMLReader functions
XMLWriter Functions
XSL functions
XSLT Functions
YAZ Functions
YP/NIS Functions
Zip File Functions
Zlib Compression Functions
eXTReMe Tracker