Zeradin Posted August 16, 2008 Share Posted August 16, 2008 I don't really know what to call this but for some reason i can read out of mysql and then when i try to do it again it doesn't work <strong class="red">Reviews</strong><br />'; while(list($id, $title) = mysql_fetch_row($result2)) { echo '<a href="reviews.php?rtyp=music&id='.$id.'">'.$title.'</a><br />'; } echo '<br /> <br /> <strong class="red">Articles</strong><br />'; while(list($id, $title) = mysql_fetch_row($result2)) { echo '<a href="reviews.php?rtyp=music&id='.$id.'">'.$title.'</a><br />'; } echo '<br /><br /> I wasn't originally trying to do the same thing twice, I was doing a $result3 thing from these: //get reviews $query2 = 'SELECT * FROM reviews WHERE reviewer = "'.$info['username'].'"'; $result2 = mysql_query($query2) or die ("error in query: $query2. ".mysql_error()); $revinfo = mysql_fetch_array($result2) or NULL; //get articles $query3 = 'SELECT * FROM articles WHERE writer = "'.$info['username'].'"'; $result3 = mysql_query($query3) or die ("error in query: $query3. ".mysql_error()); $wrtinfo = mysql_fetch_array($result3) or "error in query: $query3. ".mysql_error(); I know it's probably an obvious thing but i've been looking at it forever and can't see my mistake. Quote Link to comment https://forums.phpfreaks.com/topic/120008-solved-mysql-while-loop-thing/ Share on other sites More sharing options...
DeanWhitehouse Posted August 16, 2008 Share Posted August 16, 2008 You can only run the query once, why not change it like this <strong class="red">Reviews</strong><br />'; while(list($id, $title) = mysql_fetch_row($result2)) { echo '<a href="reviews.php?rtyp=music&id='.$id.'">'.$title.'</a><br />'; echo '<br /> <br /> <strong class="red">Articles</strong><br />'; echo '<a href="reviews.php?rtyp=music&id='.$id.'">'.$title.'</a><br />'; } echo '<br /><br /> Quote Link to comment https://forums.phpfreaks.com/topic/120008-solved-mysql-while-loop-thing/#findComment-618231 Share on other sites More sharing options...
Barand Posted August 16, 2008 Share Posted August 16, 2008 Once you get to the end of the result set you need to go back to start before you can loop through it again with mysql_data_seek ($result, 0); // rewind to row 0 Quote Link to comment https://forums.phpfreaks.com/topic/120008-solved-mysql-while-loop-thing/#findComment-618233 Share on other sites More sharing options...
DeanWhitehouse Posted August 16, 2008 Share Posted August 16, 2008 but changing it to how i had it , would be more logical. Quote Link to comment https://forums.phpfreaks.com/topic/120008-solved-mysql-while-loop-thing/#findComment-618234 Share on other sites More sharing options...
Zeradin Posted August 16, 2008 Author Share Posted August 16, 2008 but changing it to how i had it , would be more logical. but i really wanted to do this <strong class="red">Reviews</strong><br />'; while(list($id, $title) = mysql_fetch_row($result2)) { echo '<a href="reviews.php?rtyp=music&id='.$id.'">'.$title.'</a><br />'; } echo '<br /> <br /> <strong class="red">Articles</strong><br />'; while(list($id, $title) = mysql_fetch_row($result3)) { echo '<a href="articles.php?id='.$id.'">'.$title.'</a><br />'; } echo '<br /><br /> do i still have to rewind it? edit2: wait that won't do anything Quote Link to comment https://forums.phpfreaks.com/topic/120008-solved-mysql-while-loop-thing/#findComment-618237 Share on other sites More sharing options...
DeanWhitehouse Posted August 16, 2008 Share Posted August 16, 2008 try it out Quote Link to comment https://forums.phpfreaks.com/topic/120008-solved-mysql-while-loop-thing/#findComment-618238 Share on other sites More sharing options...
Zeradin Posted August 16, 2008 Author Share Posted August 16, 2008 oh i had to rewind $result3. Thanks guys! Quote Link to comment https://forums.phpfreaks.com/topic/120008-solved-mysql-while-loop-thing/#findComment-618240 Share on other sites More sharing options...
Zeradin Posted August 17, 2008 Author Share Posted August 17, 2008 hey, when i do the seek and result3 is null i get this error Warning: mysql_data_seek() [function.mysql-data-seek]: Offset 0 is invalid for MySQL result index 6 (or the query data is unbuffered) tried putting it in if loops, but didn't fix it: if ($result2 != NULL){ while(list($id, $title) = mysql_fetch_row($result2)) { echo '<a href="reviews.php?rtyp=music&id='.$id.'">'.$title.'</a><br />'; } } else { echo '<br /><br /><br />'; } if ($result3 != NULL){ mysql_data_seek ($result3, 0) or NULL; // rewind to row 0 echo '<br /> <br /> <strong class="red">Articles</strong><br />'; while(list($id, $title) = mysql_fetch_row($result3)) { echo '<a href="articles.php?rtyp=music&id='.$id.'">'.$title.'</a><br />'; } } else { echo '<br /><br /><br />'; any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/120008-solved-mysql-while-loop-thing/#findComment-618503 Share on other sites More sharing options...
Barand Posted August 17, 2008 Share Posted August 17, 2008 if offset 0 is invalid then there are no results returned. Quote Link to comment https://forums.phpfreaks.com/topic/120008-solved-mysql-while-loop-thing/#findComment-618641 Share on other sites More sharing options...
Zeradin Posted August 18, 2008 Author Share Posted August 18, 2008 yeah but i can't figure out how to make it not display that error and instead not display anything that's why i put the if ($result3 != NULL){ in there, but it still happens any idea? Quote Link to comment https://forums.phpfreaks.com/topic/120008-solved-mysql-while-loop-thing/#findComment-618846 Share on other sites More sharing options...
Andy-H Posted August 18, 2008 Share Posted August 18, 2008 $num2 = mysql_numrows($result2); if ($num2 != 0){ while(list($id, $title) = mysql_fetch_row($result2)) { echo '<a href="reviews.php?rtyp=music&id='.$id.'">'.$title.'</a><br />'; } } else { echo '<br /><br /><br />'; } $num3 = mysql_numrows($result3); if ($num3 != 0){ mysql_data_seek ($result3, 0) or NULL; // rewind to row 0 echo '<br /> <br /> <strong class="red">Articles</strong><br />'; while(list($id, $title) = mysql_fetch_row($result3)) { echo '<a href="articles.php?rtyp=music&id='.$id.'">'.$title.'</a><br />'; } } else { echo '<br /><br /><br />'; ?????? Quote Link to comment https://forums.phpfreaks.com/topic/120008-solved-mysql-while-loop-thing/#findComment-618994 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.