php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64393 dbase_replace_record(): Wrong number of fields specified
Submitted: 2013-03-08 17:06 UTC Modified: 2017-07-18 22:09 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: poulain dot mathieu at gmail dot com Assigned: cmb (profile)
Status: Closed Package: dbase (PECL)
PHP Version: 5.4.12 OS: Windows 7 64bits
Private report: No CVE-ID: None
 [2013-03-08 17:06 UTC] poulain dot mathieu at gmail dot com
Description:
------------
A simple PHP installation without other modules
php_dbase.dll version : 5.4.6

Error : PHP Warning:  dbase_replace_record(): Wrong number of fields specified

No information found since 2005 and with a old version of PHP.

I just want to replace a string on a field into a DBF file.

Test script:
---------------
$directory = "D:\Scripts";
$handler = opendir($directory);
while ($file = readdir($handler)) {
	if (strpos($file, ".DBF") == true){
		$db_path = $directory . '\\' . $file;
		$dbh = dbase_open($db_path, 2) or die("ERROR Could not open dbase database file '$db_path'.");
		$num_rec = dbase_numrecords($dbh);
		for ($i=1; $i<=$num_rec; $i++){
			$row = @dbase_get_record_with_names($dbh,$i);
			foreach ($row as $key => $val){
				$q = "'" . $val . "'";
				if (strpos($q, "TOTO") == true){
					unset($row['deleted']);
					$row[$key] = str_replace("TOTO", "TITI", $row[$key]);
					dbase_replace_record($dbh, $row, $i);
				}
			}
		}
		dbase_close($dbh);
	}
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-08-05 14:11 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2016-08-05 14:11 UTC] cmb@php.net
This is expected behavior; according to the manual[1]

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

However, you're unsetting the 'deleted' field.

[1] <http://php.net/manual/en/function.dbase-replace-record.php>
 [2016-08-06 13:09 UTC] cmb@php.net
-Status: Not a bug +Status: Re-Opened
 [2016-08-06 13:09 UTC] cmb@php.net
According to the docs unsetting the 'deleted' fields is correct,
so I'm reopening.
 [2016-08-06 15:08 UTC] cmb@php.net
-Status: Re-Opened +Status: Feedback
 [2016-08-06 15:08 UTC] cmb@php.net
The supplied test script can't work, because
dbase_replace_record() expects an *indexed* array, but $row has
been retrieved via dbase_get_record_with_names() what yields an
*associative* array. However, this should result in a "unexpected
error".

Anyhow, to analyze the issue further, we'd need a copy of a *.dbf
that causes this behavior. Please provide one for download.
 [2016-08-14 04:22 UTC] pecl-dev at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 [2017-07-17 16:58 UTC] maik-schewe at krebslauscha dot de
I also get this error always. For my understanding, this simple code should work without problems:

$db = dbase_open('TEST.DBF', 2);
$data = dbase_get_record_with_names($db, 1);
dbase_replace_record($db, $data, 1);

But it will not work. All other dbase functions works fine.

Test file: https://www.dropbox.com/s/45d6u0k08pugm87/TEST.DBF?dl=0 (file type Fox Pro 2.x standard)

Tested with PHP 7.1.7 and 5.6.31
 [2017-07-17 17:13 UTC] cmb@php.net
-Status: No Feedback +Status: Re-Opened
 [2017-07-17 17:13 UTC] cmb@php.net
> For my understanding, this simple code should work without
> problems:

No, see the first example regarding dbase_get_record_with_names(),
<http://php.net/manual/en/function.dbase-replace-record.php#refsect1-function.dbase-replace-record-examples>
 [2017-07-17 17:50 UTC] maik-schewe at krebslauscha dot de
You are absolutely right! array_values was missing. Thanks.
 [2017-07-18 19:04 UTC] maik-schewe at krebslauscha dot de
Testing again with bigger dbf file today. Code:

$db = dbase_open('../ARTIKEL.DBF', 2); // 2 = lese und schreibmodus
$count = dbase_numrecords($db);

if($db){
	for($i = 1; $i <= $count; $i++){
		$row = dbase_get_record_with_names($db, $i);
		$row['L_MATCH'] = str_replace(" ", "", $row['L_MATCH']);

		if($row['L_MATCH'] == '33254'){
			echo '<pre>';
			print_r($row);
			echo '</pre>';

			#unset($row['deleted']); //doesn't work with or without
			$row['L_VERFUEG'] = 'TEST';

			$row = array_values($row);

			if(dbase_replace_record($db, $row, $i)){echo 'true';}else{echo 'false';}

			echo '<pre>';
			print_r($row);
			echo '</pre>';
		}
	}
	dbase_close($db);
}

I have exactly 150 values before and after using array_values but this error is coming again. So the problem must be something in the loop, isn't it? Topic opener had also a loop...
 [2017-07-18 21:45 UTC] maik-schewe at krebslauscha dot de
I found the problem.

Some dbf files have field types called "Memo". I have two of them in my "big" dbf file. The Memo fields are not loaded to the "row" array with dbase_get_record_with_names, dbase_get_record should be the same. So I have in total two rows less than required, that causes the error. So this causes a bug in some cases.

Solution is for me atm, to add the missed fields to the array AND assort it new or data will be wrong in the dbf file.

Screenshot with DBF Manager: https://www.dropbox.com/s/36quhybovgud3cz/jkghjfg.png?dl=0
 [2017-07-18 22:09 UTC] cmb@php.net
-Status: Re-Opened +Status: Closed
 [2017-07-18 22:09 UTC] cmb@php.net
> Some dbf files have field types called "Memo".

Ah, of course! Indeed memo fields are not really supported[1].
While they are recognized, they are never read. Quite a while ago
I tried to add support for memo fields, but that was a bottomless
pit – there are simply to many partly subtle differences across
different versions of DBase, FoxPro, etc.

Anyhow, since this is not a bug but rather a documented
limitation, I'm closing this ticket.

[1] <http://php.net/manual/en/intro.dbase.php>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 07:01:28 2024 UTC