downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

dbase_add_record> <dba_sync
[edit] Last updated: Mon, 01 Nov 2010

view this page in

XXV. dBase 関数

導入

これらの関数は、dBase 形式(dbf) のデータベースに保存されたレコードに アクセスすることを可能にします。

dBase ファイルは、固定長の単純なシーケンシャルファイルです。 レコードはファイルの終りに追加され、削除されたレコードは dbase_pack() がコールされるまで保持されます。

dBase フィールドの型として有効なものは以下のとおりです。

表 1. 有効なフィールド型

フィールドdBase 型フォーマット補足情報
MMemon/a この型は PHP ではサポートされていません。 このフィールドは無視されます。
DDateYYYYMMDDフィールドの最大長は 8 です。
NNumber数字 長さと精度(小数点以下の桁数)を指定する必要があります。
CString文字列 長さを指定する必要があります。データを取得する際、指定した長さに 満たない文字列にはその右側に空白文字が付加されます。
LBooleanT あるいは YTRUEF あるいは NFALSE です。読み書きは整数値(1 または 0)で行われます。
FFloat浮動小数点数値この型のフィールドのサポートは PHP 5.2.0 で追加されました。

警告

これらの関数では、インデックスおよびメモフィールドのサポートは 行われません。ロックのサポートも行われません。ふたつの Web サーバ プロセスが同時に同じ dBase ファイルを修正しようとした場合、 データベースはほぼ確実に駄目になってしまうでしょう。

商用データベースとして dBase ファイルを使用しないことを推奨します。 本物の SQL サーバーを替わりに選んでください。通常、MySQL または Postgres PHP で使用されています。dBase サポートの目的は、 Web データベースのデータのインポート・エクスポートを行うことです。 なぜなら、Windows の表計算ソフトや統合ソフトでこのフォーマットが 通常サポートされているからです。

インストール手順

付属する dbase ライブラリを有効にしてこれらの関数を使用するには、 --enable-dbase オプションを 指定して PHP をコンパイルする必要があります。

実行時設定

設定ディレクティブは定義されていません。

リソース型

リソース型は定義されていません。

このリファレンスの例の多くは dBase データベースを必要とします。ここでは dbase_create() の例で作成した /tmp/test.dbf を利用しています。

定義済み定数

定数は定義されていません。

目次
dbase_add_record -- データベースにレコードを追加する
dbase_close -- データベースを閉じる
dbase_create -- データベースを作成する
dbase_delete_record -- データベースからレコードを削除する
dbase_get_header_info -- データベースのヘッダ情報を得る
dbase_get_record_with_names --  データベースからレコードを連想配列として得る
dbase_get_record --  データベースからレコードを配列形式で得る
dbase_numfields -- データベースのフィールド数を得る
dbase_numrecords -- データベースのレコード数を得る
dbase_open -- データベースをオープンする
dbase_pack -- データベースを圧縮する
dbase_replace_record -- データベースのレコードを置換する


dbase_add_record> <dba_sync
[edit] Last updated: Mon, 01 Nov 2010
 
add a note add a note User Contributed Notes dBase 関数
bi.idan [at] gmail.com 12-Feb-2009 07:16
some of you contacted me about memo fields and my script, so i'll just post my answers on the common question here.

(note: this is not an adv for any program, only what i used for myself, and it's free)

so, there are 2 options if you want to create/read memo fields:

1. use a library for java/c++/c# to build a bridge between php and dbf file. i've used dbf2java. althougth it's not near complete as my other option, it's a good start. (http://code.google.com/p/dbf2java-library/)

2. use an external program with pipes to grab the output. i used cdbflite, (http://www.whitetown.com/cdbflite/). it's free, and pretty much gives you everything you need to handle those dbf files. i'm not sure about big databases and it seems you need to register/buy it, but it's again, a start.

hope it helps some of you who needs to handle those database files,

Good luck,
idan
kuthullu at webmails dot com 09-Apr-2008 07:40
For all those of you who wants to DBF->SQL, here's half of a hour worth of work.
It's pretty simple, outputs a CREATE TABLE and lots of INSERTs.
The output is HTML, as I use w$ndows here at work and prefer to just see results in opera.
You SHOULD verify the created data, add some CREATE KEYs, NOT NULLs or whatever you like.

<?php
$file_name
= "mfiscal/dados22/APISS110.D00";
$table = "dbf_apiss";

$dbf = @dbase_open($file_name, 0) or die("Error opening $file_name");
$fields = dbase_get_header_info($dbf);

/* Just show a table with header information */
echo "<table cellspacing=0><tr><th>Name</th><th>Type</th>
<th>Length</th><th>Precision</th><th>Format</th><th>Offset</th></tr>"
;
foreach(
$fields as $field)
{
    echo
"<tr>
        <td>{$field['name']}</td>
        <td>{$field['type']}</td>
        <td>{$field['length']}</td>
        <td>{$field['precision']}</td>
        <td>{$field['format']}</td>
        <td>{$field['offset']}</td>
        </tr>"
;
}
echo
"</table><br><br>";

/* Here begins the SQL code part */
echo "CREATE TABLE $table (<BR>";
$x = 0;
$fields_num = 0;
foreach(
$fields as $field)
{
    if(
$x++ != 0)
        echo
",<BR>";
    switch(
$field['type'])
    {
        case
'character' : $type = 'CHAR'; $length = $field['length'] > 1 ? "({$field['length']})" : ""; break;
        case
'number' : $type = 'NUMERIC'; $length = "({$field['length']}" . ($field['precision'] > 0 ? ", {$field['precision']})": ")");
        break;
//        case '' : $type = ''; break;
   
}
   
$fields_num++;
    echo
"&nbsp;&nbsp;&nbsp;{$field['name']} $type$length";
}
echo
");<BR><BR>BEGIN;<BR>";
$records = @dbase_numrecords($dbf) or die("Error reading DBF's number of fields");
for(
$x = 1; $x <= $records; $x++)
{
   
$record = dbase_get_record($dbf, $x);
    echo
"INSERT INTO $table VALUES (";
   
$f = 0;
    foreach(
$record as $field)
    {
        if(
$f != 0) echo ", ";
        if(
strcmp(str_repeat(' ', $fields[$f]['length']), $field) != 0)
        {
            if(
$fields[$f]['type'] == 'character') echo "'";
            echo
$field;
            if(
$fields[$f]['type'] == 'character') echo "'";
        }
        else
            echo
"NULL";
        if(++
$f >= $fields_num) break;
    }
    echo
");<BR>";
}
echo
"COMMIT;";
?>
bi.idan AT gmail.com 16-Apr-2007 04:40
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;
    }
}

?>
Hadi Rusiah / deegos at yahoo dot com 08-May-2004 10:33
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);
?>

 
show source | credits | sitemap | contact | advertising | mirror sites