jaymc Posted October 24, 2006 Share Posted October 24, 2006 Here is my code[code]$querya = "SELECT * FROM `messages` WHERE `TO` = '$User_Session' ORDER BY `DATE` DESC LIMIT 0,10";$runquerya = mysql_query($querya);[/code]Now, I am using a while loop in conjuction with a mysql array fetch to retrieve the rows, like so..[code]while ($resultsa = mysql_fetch_array($runquerya)) {echo $resultsa[FROM];}[/code]That works fine, however, if I then run the same loop again in the same script, it wont work. like so..[code]while ($resultsb = mysql_fetch_array($runquerya)) {echo $resultsb[FROM];}[/code]The only way I can get it to work is if I run the same mysql_query() twice but store the results in different variables, likes so..[code]$querya = "SELECT * FROM `messages` WHERE `TO` = '$User_Session' ORDER BY `DATE` DESC LIMIT 0,10";$runquerya = mysql_query($querya);$runqueryb = mysql_query($querya);[/code]And then do a mysql_fetch_array for the first, then the second. That works. But obviously Its not the correct way to go around it. Im just wondering why this is happening? Its as if once running a mysql_fetch_array() its emptying out $runquerya meaning it cant be used again?Completely lost... any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/24912-only-1-fetch/ Share on other sites More sharing options...
Jenk Posted October 24, 2006 Share Posted October 24, 2006 you have an error in your syntax..[code=php:0]$resultsb['FROM'][/code] Quote Link to comment https://forums.phpfreaks.com/topic/24912-only-1-fetch/#findComment-113543 Share on other sites More sharing options...
jaymc Posted October 24, 2006 Author Share Posted October 24, 2006 It still works though... without the literalsBut the problem explained about happens with or without the ' ' Quote Link to comment https://forums.phpfreaks.com/topic/24912-only-1-fetch/#findComment-113549 Share on other sites More sharing options...
HuggieBear Posted October 24, 2006 Share Posted October 24, 2006 This is because there's an internal pointer that gets moved on each time a row is accessed. So if you use a while loop to access all the rows, the next time you try to get a row (by creating another while loop) the pointer is at the end of the result set.Try adding this line after the first while loop:[code=php:0]mysql_data_seek($runquerya, 0);[/code]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/24912-only-1-fetch/#findComment-113550 Share on other sites More sharing options...
Jenk Posted October 24, 2006 Share Posted October 24, 2006 [quote author=jaymc link=topic=112511.msg456709#msg456709 date=1161686476]It still works though... without the literalsBut the problem explained about happens with or without the ' '[/quote]The solution has been explained above, but in regards to it working "fine" without the literals, that's not entirely true. I can tell just from the post I've quoted you do not have error reporting set to include E_NOTICE, else you will see many, many error messages. Quote Link to comment https://forums.phpfreaks.com/topic/24912-only-1-fetch/#findComment-113552 Share on other sites More sharing options...
jaymc Posted October 24, 2006 Author Share Posted October 24, 2006 [quote author=HuggieBear link=topic=112511.msg456710#msg456710 date=1161686494]This is because there's an internal pointer that gets moved on each time a row is accessed. So if you use a while loop to access all the rows, the next time you try to get a row (by creating another while loop) the pointer is at the end of the result set.Try adding this line after the first while loop:[code=php:0]mysql_data_seek($runquerya, 0);[/code]RegardsHuggie[/quote]That worked a treat! Thanks!! Quote Link to comment https://forums.phpfreaks.com/topic/24912-only-1-fetch/#findComment-113556 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.