php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #34911 php_dbase.dll : dbase_replace_record : tricks to work
Submitted: 2005-10-18 19:52 UTC Modified: 2005-10-22 18:43 UTC
From: allan dot s dot oliveira at gmail dot com Assigned:
Status: Not a bug Package: dBase related
PHP Version: 5.0.5 OS: Windows NT WXP 5.1 build 2600
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: allan dot s dot oliveira at gmail dot com
New email:
PHP Version: OS:

 

 [2005-10-18 19:52 UTC] allan dot s dot oliveira at gmail dot com
Description:
------------
the last bug report was 2001... but, here it goes.

if you get the record and simply try to replace it using the loaded array, the number of arguments is invalid.

Reproduce code:
---------------
<?php
$dbname = "./tmp/test.dbf"; // dbf name
$def = array( array("date", "D"), array("name", "C", 50), array("age", "N", 3, 0), 
array("email", "C", 128), array("ismember", "L") ); // structure
dbase_create($dbname, $def) // creating
$db=dbase_open($dbname,2) // open to insert
$reg=array("20030912","My Name","33","example@example.com",False); // define record
dbase_add_record($db,$reg) // insert record
dbase_close($db); // close
$db=dbase_open($dbname,2) // open to replace
$row = dbase_get_record_with_names($db, 1); // gets actual row
$row['email'] = "newmail@newmail.com"; // Update the date field with the current timestamp
dbase_replace_record($db,$row,1); // replace
// above instruction causes an error : incorrect number of arguments
$reg = array( $row['date'] , $row['name'] , $row['age'] , $row['email'] , $row['ismember'] ); // redefine record without 'deleted' column 
dbase_replace_record($db,$reg,1); // Replace the record : OK
// This Replace goes OK
dbase_close($db);
echo "Done";
?>

Expected result:
----------------
this should work properly, without any tricks :

$row = dbase_get_record_with_names($db, 1)
$row['email'] = "newmail@newmail.com";
dbase_replace_record($db,$row,1);



Actual result:
--------------
I recently start to work with some dbase files, and following the examples, this error should not appear.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-10-21 23:13 UTC] tony2001@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

dbase_get_record*() funcs return a row and add "deleted" field to indicate if this row were marked for deletion or not.
You can see a note about it in dbase_replace_record() reference:
"Note: If you're using dbase_get_record() return value for this parameter, remember to reset the key named deleted."

Also, dbase_replace/add_record() work only with indexed arrays and this is documented too.
 [2005-10-22 18:40 UTC] allan dot s dot oliveira at gmail dot com
Thanks Tony2001 in advance.

If I understood the explanation, it means that I should use dbase_get_record() instead of dbase_get_record_with_names() as it is in the "Example 438. Updating a record in the database" in the section "Function Reference / dBase Functions".

I tried again as the PHP Manual Example with some changes :
1. dbase_get_record instead of dbase_get_record_with_names
2. '0' for 'date'

Even then, the get_record followed by replace_record did not works as it should.
The 'dbase_get_record()' gave me an indexed array, but the same error occurs :
PHP Warning:  dbase_replace_record(): unexpected error in D:\Apache\Apache2\htdocs\tests\_debug_tmp.php on line 17

No problem, I created a function to execute the replace that recreates an indexed array and take the extra 'deleted' element off.
$rarr = array();
foreach ($row as $i => $vl) { if ($i <> "deleted") { $rarr[] = $vl; } }

I believe that this transformation should be inside the dbase_replace_record, and not so simple like mine, but considering the table structure crossed with the array elements.

Best Regards,
 [2005-10-22 18:43 UTC] allan dot s dot oliveira at gmail dot com
Sorry, the correct example should be :

unset($row['deleted']);
$rarr = array();
foreach ($row as $i => $vl) { $rarr[] = $vl; }

and then replace_record using rarr.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 16:01:28 2024 UTC