stubarny Posted August 16, 2012 Share Posted August 16, 2012 Hello, I have 2 loops that use the following code: while($record = mysql_fetch_array($result_user_votes, MYSQL_BOTH) The first time loop works, the second loop doesn't work at all - does this mean that I can't use the SQL results array more than once? or am I going about this incorrectly? Thanks, Stu Quote Link to comment https://forums.phpfreaks.com/topic/267190-looping-through-sql-results/ Share on other sites More sharing options...
MMDE Posted August 16, 2012 Share Posted August 16, 2012 I'm not 100% sure why, but it might just not restart the list after the while loop has run. You may want to use mysql_data_seek to reset the pointer to the beginning again. In any case, why would you need to loop through it twice? Quote Link to comment https://forums.phpfreaks.com/topic/267190-looping-through-sql-results/#findComment-1369957 Share on other sites More sharing options...
rythemton Posted August 16, 2012 Share Posted August 16, 2012 The MySQL fetch functions have an internal pointer that advances each time you fetch a row. If you need to parse the data again, you'll have to move the pointer back to the beginning using the mysql_data_seek function: mysql_data_seek( $result_user_votes, 0 ); Quote Link to comment https://forums.phpfreaks.com/topic/267190-looping-through-sql-results/#findComment-1369960 Share on other sites More sharing options...
stubarny Posted August 16, 2012 Author Share Posted August 16, 2012 awesome, thanks guys - that worked The reason for needing 2 loops: first loop is for a 'history' list of votes a user has made, the second loop is within a list of posts so that only posts the user has not voted on yet are displayed. Would the below code be better? ------------------------ $record = mysql_fetch_array($result_user_votes, MYSQL_BOTH); and then two loops of: while($record) -------------------------- Stu Quote Link to comment https://forums.phpfreaks.com/topic/267190-looping-through-sql-results/#findComment-1369963 Share on other sites More sharing options...
xyph Posted August 16, 2012 Share Posted August 16, 2012 No. You should perform both functions in the single loop. Quote Link to comment https://forums.phpfreaks.com/topic/267190-looping-through-sql-results/#findComment-1369966 Share on other sites More sharing options...
rythemton Posted August 18, 2012 Share Posted August 18, 2012 No. You should perform both functions in the single loop. I agree with xyph, doing both functions in a single loop is more efficient and would the preferred method. Quote Link to comment https://forums.phpfreaks.com/topic/267190-looping-through-sql-results/#findComment-1370483 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.