PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » Database » DB » Bug #7502

DB_result::fetchRow() that sets $rownum still increments row_counter

Details

Submitted2006-04-27 13:36 UTC
Fromtrollll at frozen-o dot com
Assignedaharvey
StatusClosed
PackageDB
PHP Version5.1.2
OSMacOS 10.4
Roadmaps(Not assigned)

Comments

[2006-04-27 13:36 UTC] trollll at frozen-o dot com

Description:
------------
In several places, I run a query and then reference the result object in more than one place. Every time, I use a database object layer to retrieve each row by number.

However, at a certain point, fetchRow starts returning null as if I had reached the limit set by the query, even if I request row number 7 of 8.

Test script:
---------------
<?php
// Using the test database from MySQL 5

require_once 'DB.php';

$dsn = 'mysqli://test:test@localhost/test';
$options = array(
'debug' => 2,
'portability' => DB_PORTABILITY_ALL,
);

$db =& DB::connect($dsn, $options);
if (PEAR::isError($db)) {
die($db->getMessage());
}

// Once you have a valid DB object named $db...
$res =& $db->limitQuery('SELECT name FROM testaa', 0, 3);
for ($i = 0; $i < 10; $i++) {
$row =& $res->fetchRow(DB_FETCHMODE_ORDERED, 1);
echo $row[0] . "\n";
}
?>

Quick 'n' dirty fix in DB.php, starting at line 1092:

- if ($this->limit_from !== null) {
+ if (is_null($rownum) && $this->limit_from !== null) {
if ($this->row_counter === null) {
$this->row_counter = $this->limit_from;
// Skip rows
if ($this->dbh->features['limit'] === false) {
$i = 0;
while ($i++ < $this->limit_from) {
$this->dbh->fetchInto($this->result, $arr, $fetchmode);
}
}
}
if ($this->row_counter >= ($this->limit_from + $this->limit_count))
{
if ($this->autofree) {
$this->free();
}
$tmp = null;
return $tmp;
}
if ($this->dbh->features['limit'] === 'emulate') {
$rownum = $this->row_counter;
}
$this->row_counter++;
}

Expected result:
----------------
Jochen Wiedmann
Jochen Wiedmann
Jochen Wiedmann
Jochen Wiedmann
Jochen Wiedmann
Jochen Wiedmann
Jochen Wiedmann
Jochen Wiedmann
Jochen Wiedmann
Jochen Wiedmann

Actual result:
--------------
Jochen Wiedmann
Jochen Wiedmann
Jochen Wiedmann