// simply use 'dbase_get_record' instead of 'dbase_get_record_with_names', for 'dbase_replace_record', 'dbase_add_record', or 'dbase_delete_record', to work
<?php
// open in read-write mode
$db = dbase_open('/tmp/test.dbf', 2) or die("Error! Could not open dbase database file /tmp/test.dbf."); // 0=RO, 1=WO, 2=RW
// gets the old row
$row = dbase_get_record($db, 1);
// remove the 'deleted' entry from the array
unset($row['deleted']);
// Update the date field with the current timestamp
$row['date'] = date('Ymd');
// Replace the record
dbase_replace_record($db, $row, 1);
dbase_close($db);
?>
dbase_replace_record
(PHP 4, PHP 5)
dbase_replace_record — データベースのレコードを置換する
説明
bool dbase_replace_record
( int
$dbase_identifier
, array $record
, int $record_number
)データベースの指定したレコードを、指定した値で置換します。
パラメータ
-
dbase_identifier -
データベースのリンク ID 。dbase_open() あるいは dbase_create() によって返されます。
-
record -
データの配列。要素の数がデータベースのフィールド数と一致している 必要があります。それ以外の場合はdbase_replace_record() は失敗します。
注意:
dbase_get_record() が返す値をこのパラメータに 使用する際は、deleted という名前のキーを リセットすることを忘れないでください。
-
record_number -
1 からデータベース内のレコード数 (dbase_numrecords() が返す) までの範囲の整数値。
返り値
成功した場合に TRUE を、失敗した場合に FALSE を返します。
例
例1 データベースのレコードの更新
<?php
// read-write モードでオープンする
$db = dbase_open('/tmp/test.dbf', 2);
if ($db) {
// 変更前の行を取得する
$row = dbase_get_record_with_names($db, 1);
// 'deleted' エントリを削除する
unset($row['deleted']);
// 現在のタイムスタンプでフィールドを更新する
$row['date'] = date('Ymd');
// レコードを置換する
dbase_replace_record($db, $row, 1);
dbase_close($db);
}
?>
xamkan at yahoo dot com
20-Sep-2010 04:43
3Famous at gmail dot com
11-Feb-2008 09:03
Actually you have to change the read array from key based to element (numeric) based and everything works correctly!
<?php
$db = dbase_open( "yourfile.dbf", 2); // 0=RO, 1=WO, 2=RW
if ($db) {
$row = dbase_get_record_with_names($db, 1);
unset($row["deleted"]); // drop the field
// do whatever it is you want to the $row["elements"]
// then convert to numeric to store:
$rarr = array();
foreach ($row as $i=>$vl) $rarr[] = $vl;
dbase_replace_record($db, $rarr, 1);
dbase_close($db);
}
?>
That code actually works!
hassan at datakillarna dot se
10-Feb-2006 04:30
If you get "unexpected error", try to change the assoc array, $row, to an indexed array with array_values().
Example:
<?php
$row = array_values($row);
dbase_replace_record($db, $row, 1);
?>
The dbase_replace-function cannot handle an assoc array.
Hope this will save someone a headache! ;)
wysocki at wildworld dot net
11-Feb-2005 01:15
The dbase add and replace functions do NOT like to use the associative array.
<?php
//This gives "unspecified error" on replace and add:
$row = dbase_get_record_with_names($db, 1);
unset($row['deleted']);
dbase_replace_record($db, $row, 1);
dbase_add_record($db, $row);
//To further prove the point,
//The first add works, the second one fails:
$testrow1=array('one','2','three');
dbase_add_record($db,$testrow1);
$testrow2=array('FIELDA' => 'xxx','FIELDB' => '9','FIELDC' => 'yyyyy');
dbase_add_record($db,$testrow2);
?>
