Re: Cache:DB skipping first row in MySQL result
| From: | Brian Busche | Date: | Mon, 28 Apr 2003 18:11:10 +0000 |
| Subject: | Re: Cache:DB skipping first row in MySQL result | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-5105@lists.php.net to get a copy of this message | ||
I'm not much of a programmer, but it seems to me that one would never get the first row of a
cached query using 'fetchRow()' because the $this->cursor increments to 1 before we
return the record at cursor position 0
I was able to get around this by making the following changes in the DB.php, although its not pretty
(told you I'm not much of a programmer). See comments for my reasoning.
function fetchRow($fetchmode = DB_FETCHMODE_DEFAULT, $rownum = null) {
if ($fetchmode === DB_FETCHMODE_DEFAULT) {
$fetchmode = $this->fetchmode;
}
if ($fetchmode === DB_FETCHMODE_OBJECT) {
$fetchmode = DB_FETCHMODE_ASSOC;
$return_object = true;
}
if ($rownum === null) {
// Removed this line because I need cursor position 0 first
//$this->cursor++;
} else {
$this->cursor = $rownum;
}
if ($rownum < sizeof($this->rows)) {
// Added this line because I need the cursor to be 0 only once
if(!$this->cursor > 1){$this->cursor = 0;}
$row = $this->rows[$this->cursor];
// I moved the cursor incement call to here
$this->cursor++;
} else {
return false;
}
Best Regards,
Brian Busche
President
Rendition Multimedia
500 W. Central Road
Suite 208
Mount Prospect, IL. 60056
brian@archconsltd.com
----- Original Message -----
From: Brian Busche
To: pear-general@lists.php.net
Sent: Monday, April 28, 2003 11:53 AM
Subject: [PEAR] Cache:DB skipping first row in MySQL result
Has anyone encountered this with PEAR:Cache 1.5.3 ?
Looping over the result set is skipping the first result in a MySQL query. I am using PHP
4.3.1, Cache 1.5.3, and my code is fairly straight forward:
require 'Cache/DB.php';
$age = (3*3600);
$cache = new Cache_DB('file',array('cache_dir' =>
'/tmp/','filename_prefix' => 'query_'),$age);
$cache->connect('mysql://root:password@192.168.100.2/table');
$sql = "SELECT uname FROM users WHERE lasthit > (NOW() - INTERVAL 1 DAY)";
$sth = $cache->query($sql);
while($row = $sth->fetchRow(DB_FETCHMODE_ASSOC)){
extract($row);
print "User is $user<br>";
}
Best Regards,
Brian Busche
President
Rendition Multimedia
500 W. Central Road
Suite 208
Mount Prospect, IL., 60056
brian@archconsltd.com