dBase 函数
在线手册:中文  英文

dbase_replace_record

(PHP 4, PHP 5)

dbase_replace_recordReplaces a record in a database

说明

bool dbase_replace_record ( int $dbase_identifier , array $record , int $record_number )

Replaces the given record in the database with the given data.

参数

dbase_identifier

The database link identifier, returned by dbase_open() or dbase_create().

record

An indexed array of data. The number of items must be equal to the number of fields in the database, otherwise dbase_replace_record() will fail.

Note:

If you're using dbase_get_record() return value for this parameter, remember to reset the key named deleted.

record_number

An integer which spans from 1 to the number of records in the database (as returned by dbase_numrecords()).

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

Example #1 Updating a record in the database

<?php

// open in read-write mode
$db dbase_open('/tmp/test.dbf'2);

if (
$db) {
  
// gets the old row
  
$row dbase_get_record_with_names($db1);
  
  
// remove the 'deleted' entry
  
unset($row['deleted']);
  
  
// Update the date field with the current timestamp
  
$row['date'] = date('Ymd');
  
  
// Replace the record
  
dbase_replace_record($db$row1);
  
dbase_close($db);
}

?>

参见


dBase 函数
在线手册:中文  英文

用户评论:

rlundp at lunds dot us (2013-01-07 22:48:40)

It took me a while to figure this out, so even though this is near-obsolete, maybe it will be useful to someone else.
I have a dBase file that contains a boolean value. I created a new row in the file with the boolean value at 0, and that worked without issues.
But then I tried to set that value to 1, and it did not work. I tried 1, "1", true - all without success.
Turns out that you need to use "Y" to set a boolean value to 1.
When you load the row later and output it using var_dump, the value will show as integer with the value 1.

xamkan at yahoo dot com (2010-09-20 16:43:14)

// 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($db1);

// 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$row1);
dbase_close($db);

?>

3Famous at gmail dot com (2008-02-11 09:03:41)

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($db1);
        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$rarr1);
        
dbase_close($db);
    }
?>

That code actually works!

hassan at datakillarna dot se (2006-02-10 16:30:17)

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$row1);
?>

The dbase_replace-function cannot handle an assoc array.

Hope this will save someone a headache! ;)

wysocki at wildworld dot net (2005-02-11 13:15:10)

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($db1);
unset(
$row['deleted']);
dbase_replace_record($db$row1);
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);
?>

易百教程