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.
dbase_replace_record
(PHP 4, PHP 5)
dbase_replace_record — Reemplaza un registro en una base de datos
Descripción
$dbase_identifier
, array $record
, int $record_number
)Reemplaza el registro dado en la base de datos con la información dada.
Parámetros
-
dbase_identifier -
El identificador de enlace de la base de datos, devuelto por dbase_open() o dbase_create().
-
record -
Ina matriz indexada de información. El número de elementos debe ser igual que el número de campos de la base de datos, de otro modo dbase_replace_record() fallará.
Nota:
Si se está usando el valor devulto por dbase_get_record() para este parámetro, recuerde de reiniciar la clave llamada deleted.
-
record_number -
Un entero que abarca desde 1 hasta el número de registros de la base de datos (como el devuelto por dbase_numrecords()).
Valores devueltos
Devuelve TRUE en caso de éxito o FALSE en caso de error.
Ejemplos
Ejemplo #1 Actualizar un registro en la base de datos
<?php
// abrir en modo lectura-escritura
$db = dbase_open('/tmp/test.dbf', 2);
if ($db) {
// obtiene la fila antigua
$fila = dbase_get_record_with_names($db, 1);
// eliminar la entrada 'deleted'
unset($fila['deleted']);
// Actualizar el campo de fecga con la fecha actual
$fila['date'] = date('Ymd');
// Reemplazar el registro
dbase_replace_record($db, $fila, 1);
dbase_close($db);
}
?>
Ver también
- dbase_add_record() - Añade un registro a una base de datos
- dbase_delete_record() - Borra un registro de una base de datos
// 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);
?>
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!
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! ;)
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);
?>
