Jump to content
MakeWebGames

Recycle Query Results


UCC

Recommended Posts

So lets say you have a query

$q=$db->query("");

while ($r=$db->fetch_row($q))

{

}

Now, lets say I need to re-use the Exact Same query results after that. However, since we already looped through $q, you can't simply loop through it a second time.

I've tried $q2=$q before the loop but that doesn't work. That loop doesnt go through, it's like an empty dataset.

I even tried reset($q); but the error sayd $q was not an array.

I'd really prefer not to repeat the exact same query if I dont have to.

UCC

Link to comment
Share on other sites

Re: Recycle Query Results

 

So lets say you have a query

$q=$db->query("");

while ($r=$db->fetch_row($q))

{

}

Now, lets say I need to re-use the Exact Same query results after that. However, since we already looped through $q, you can't simply loop through it a second time.

I've tried $q2=$q before the loop but that doesn't work. That loop doesnt go through, it's like an empty dataset.

I even tried reset($q); but the error sayd $q was not an array.

I'd really prefer not to repeat the exact same query if I dont have to.

UCC

 

When you do the query, the results are stored in the variable $q. $q itself is not changed again (even throughout the loop), so why can't you simply use it again?

(There's a good chance I'm missing something here, or there's some factor I don't fully understand)

Link to comment
Share on other sites

Re: Recycle Query Results

Based on what you said, here is what I tried. Negative results. First loop was fine, second loop did nothing.

 

$q=$db->query("SELECT * FROM inventory WHERE inv_userid = 378");
while($r=$db->fetch_row($q))
{
print_r($r);
}

print "

xxxx

";


while($r=$db->fetch_row($q))
{
print_r($r);
}
Link to comment
Share on other sites

Re: Recycle Query Results

 

$q=$db->query("SELECT * FROM inventory WHERE inv_userid = 378");
while($r=$db->fetch_row($q))
{
print_r($r);
}

print "

xxxx

";
mysql_data_seek($q, 0);

while($r=$db->fetch_row($q))
{
print_r($r);
}

 

The resource ($q) was set to the end of the rows, so it was basically trying to loop from the end, that 'seeks' it back to row 0 (just learnt it now)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...