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?
How to fetchrow without messing the $db variable resource?
Moderator: General Moderators
- kaisellgren
- DevNet Resident
- Posts: 1675
- Joined: Sat Jan 07, 2006 5:52 am
- Location: Lahti, Finland.
Re: How to fetchrow without messing the $db variable resource?
Paste us some code and we'll take a look!
- kaisellgren
- DevNet Resident
- Posts: 1675
- Joined: Sat Jan 07, 2006 5:52 am
- Location: Lahti, Finland.
Re: How to fetchrow without messing the $db variable resource?
After query:
Then I have the $db -> fetchrow() -function.
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 =/
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...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;
}Re: How to fetchrow without messing the $db variable resource?
Code: Select all
$sqlresult = isset($result) ? $result : $this -> result;
mysql_data_seek($sqlresult,0); //reset the mysql cursor to the first row.
- kaisellgren
- DevNet Resident
- Posts: 1675
- Joined: Sat Jan 07, 2006 5:52 am
- Location: Lahti, Finland.
Re: How to fetchrow without messing the $db variable resource?
I tried the data_seek, and then I got this error whenever I have mysqli_data_seek($sqlresult,0);
Here's the code:
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 172Code: 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;
}
}