#15 Patch for bug in $rs->GetAssoc

open
nobody
None
5
2015-02-25
2006-07-04
jrf
No

Case:
When a resultset with associate keys contains numeric
keys, GetAssoc will - because of the use of the
array_slice function - renumber the numeric keys.

Based on the php documentation
(http://www.php.net/manual/en/function.array-slice.php),
I suspect this behaviour has been changed for php5.0.2
and up, but older php version will show this
(undesired) behaviour.

Any function which calls the $rs->GetAssoc function
internally, such as $db->GetAssoc, will show the same
bugged results.

Typical example:
A Pivottable which pivots on a numeric field

$sql = PivotTableSQL(
$db, # adodb connection
'students', # tables
'school', # rows (multiple fields allowed)
'entry_year' # column to pivot on
);

Query result:
School 1996 1997 Total
School1 10 20 30
School2 5 80 85

GetAssoc result
School1 =>
[0] => 10
[1] => 20
[Total] => 30
School2 =>
[0] => 5
[1] => 80
[Total] => 85

Patch:
Based on suggestions made in the comments in the PHP
documentation.

Patch based on adodb v4.90.
Tested on php 4.4.0.

Find line 2876 and 2888 in adodb.inc.php - both should
currently be:
$results[trim(reset($this->fields))] =
array_slice($this->fields, 1);

Replace both lines with the following block of code:
// Fix for array_slice re-numbering numeric associative
keys
$keys = array_slice(array_keys($this->fields), 1);
$sliced_array = array();
foreach($keys as $key) {
$sliced_array[$key] = $this->fields[$key];
}
$results[trim(reset($this->fields))] = $sliced_array;

Patch performance impact:
This *is* slower than using the native array_slice, but
not by much.
Benchmarked running 1000 x on the result of a 15 record
pivot table - performance difference 0.015 seconds (for
the whole set of 1000 runs).

Other information:
Report in phplens forum:
Bug report:

Discussion

  • jrf
    jrf
    2006-07-04

    patched version of adodb.inc.php v4.90

     
    Attachments
  • jrf
    jrf
    2006-07-04

    file showing only the patched $rs->GetAssoc function