How to fetchrow without messing the $db variable resource?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

How to fetchrow without messing the $db variable resource?

Post 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?
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Re: How to fetchrow without messing the $db variable resource?

Post by panic! »

Paste us some code and we'll take a look!
User avatar
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?

Post 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 8) the mysqli_fetch_assoc() does not work anymore, because I already used the fetch_assoc() in the cache class. The caching works great though =/
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Re: How to fetchrow without messing the $db variable resource?

Post by panic! »

Code: Select all

 
$sqlresult = isset($result) ? $result : $this -> result;
 
mysql_data_seek($sqlresult,0); //reset the mysql cursor to the first row.
 
 
User avatar
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?

Post 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;
     }
   }
Post Reply