Page 1 of 1
How to fetchrow without messing the $db variable resource?
Posted: Tue Sep 09, 2008 7:20 am
by kaisellgren
Hi,
I have a class that caches my SQL results, basically if I cache an SQL result, I loop through it using fetchrow() - then I save it to a file.
Later if I try to loop through the result using fetchrow() again, it does not work anymore, so how would I resolve this problem?
Re: How to fetchrow without messing the $db variable resource?
Posted: Tue Sep 09, 2008 7:41 am
by panic!
Paste us some code and we'll take a look!
Re: How to fetchrow without messing the $db variable resource?
Posted: Tue Sep 09, 2008 7:53 am
by kaisellgren
After query:
Code: Select all
if ($cachettl)
$cache -> cache_sql($query,$this -> result,$cachettl);
Code: Select all
public function cache_sql($query,$result,$cachettl)
{
global $db;
...
$sqlrows = array();
while ($row = $db -> fetchrow($result))
$sqlrows[] = $row;
... // it writes the $sqlrows into a file as an array...
Then I have the $db -> fetchrow() -function.
Code: Select all
public function fetchrow($result = NULL)
{
global $cache;
$sqlresult = isset($result) ? $result : $this -> result;
if (is_array($sqlresult))
return $cache -> cached_sql_fetchrow($sqlresult);
else
return ($sqlresult !== false) ? mysqli_fetch_assoc($sqlresult) : false;
}
The problem is that in the last code (line

the mysqli_fetch_assoc() does not work anymore, because I already used the fetch_assoc() in the cache class. The caching works great though =/
Re: How to fetchrow without messing the $db variable resource?
Posted: Tue Sep 09, 2008 8:21 am
by panic!
Code: Select all
$sqlresult = isset($result) ? $result : $this -> result;
mysql_data_seek($sqlresult,0); //reset the mysql cursor to the first row.
Re: How to fetchrow without messing the $db variable resource?
Posted: Tue Sep 09, 2008 1:52 pm
by kaisellgren
I tried the data_seek, and then I got this error whenever I have mysqli_data_seek($sqlresult,0);
Code: Select all
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 63 bytes) in C:\Program Files\Apache\htdocs\tcms\core\classes\mysqli.php on line 172
Here's the code:
Code: Select all
public function fetchrow($result = NULL)
{
global $cache;
$sqlresult = isset($result) ? $result : $this -> result;
if (is_array($sqlresult)) // It's an array, so it's generated by cache (taken from a cache file)
return $cache -> cached_sql_fetchrow($sqlresult);
else // It's not an array, it's whether false (no matches) or a mysqli resource containing resultset.
{
mysqli_data_seek($sqlresult,0);
return ($sqlresult !== false) ? mysqli_fetch_assoc($sqlresult) : false;
}
}