UCC Posted August 25, 2007 Share Posted August 25, 2007 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 Quote Link to comment Share on other sites More sharing options...
seanybob Posted August 25, 2007 Share Posted August 25, 2007 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) Quote Link to comment Share on other sites More sharing options...
UCC Posted August 25, 2007 Author Share Posted August 25, 2007 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); } Quote Link to comment Share on other sites More sharing options...
Decepti0n Posted August 25, 2007 Share Posted August 25, 2007 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) Quote Link to comment Share on other sites More sharing options...
UCC Posted August 25, 2007 Author Share Posted August 25, 2007 Re: Recycle Query Results Thats why I was trying to use reset() to move the pointer. Thanks Deception Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.