FlashNinja Posted April 20, 2012 Share Posted April 20, 2012 I've develped this small script to display user reviews stored in a my databases review table. The problem with the script though is that it seems to be looping ad nauseum. The wierd thing though, is that I've used a very similair script to display another list of items on this site, and it worked correctly without any issue. Could someone take a look at this for me and diagnose the error with the script. The SQL table: CREATE TABLE IF NOT EXISTS `rev` ( `id` int(11) NOT NULL AUTO_INCREMENT, `rev_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, `usr_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, `text` varchar(600) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ; The PHP script: User reviews for <? echo $member;?> <br> <table border="0"> <?php for($count = 1; $count <= $revrows; $count++) { $sqlrev = "SELECT * FROM rev WHERE rev_name = '$member' ORDER BY id DESC"; $revresult = mysql_query($sqlrev) or die(mysql_error()); $revrows = mysql_fetch_row($revresult); $revname = mysql_fetch_array($revresult); ?> <tr> <?php print "Review by: " . $revname['usr_name'] . "<br>". $revname['text']; ?> </tr> <?php } ?> </table> Quote Link to comment https://forums.phpfreaks.com/topic/261339-review-script/ Share on other sites More sharing options...
batwimp Posted April 20, 2012 Share Posted April 20, 2012 You should put your database queries, etc... BEFORE your for loop. Quote Link to comment https://forums.phpfreaks.com/topic/261339-review-script/#findComment-1339205 Share on other sites More sharing options...
FlashNinja Posted April 20, 2012 Author Share Posted April 20, 2012 I did that, still having the same issue. It just keeps repeating until the browser crashes. It's really wierd none of my other FOR loops are doing this and this one is entirely baed off of the functioning ones. Quote Link to comment https://forums.phpfreaks.com/topic/261339-review-script/#findComment-1339209 Share on other sites More sharing options...
batwimp Posted April 20, 2012 Share Posted April 20, 2012 Echo out your $count and $revrows to see what they are. Quote Link to comment https://forums.phpfreaks.com/topic/261339-review-script/#findComment-1339210 Share on other sites More sharing options...
FlashNinja Posted April 20, 2012 Author Share Posted April 20, 2012 I've echoed them outside the FOR loop and within it. Outside of it they echo as they should - 1, ARRAY - but trying to echo them within the FOR loop causes an infinite loop still. It seems to be the requirements for the loop to start that is causing this infinite loop, the wierd thing is that the other FOR loops I've used have had the same requirements. Quote Link to comment https://forums.phpfreaks.com/topic/261339-review-script/#findComment-1339212 Share on other sites More sharing options...
batwimp Posted April 20, 2012 Share Posted April 20, 2012 Right. But were you able to tell if the requirements of loop termination have been met by the variables from within the loop? You would have to break out yourself (or put a different counter inside that loop that would break out), but you should still be able to see if the variables are filling up correctly. Quote Link to comment https://forums.phpfreaks.com/topic/261339-review-script/#findComment-1339213 Share on other sites More sharing options...
FlashNinja Posted April 20, 2012 Author Share Posted April 20, 2012 I've worked on it a bit more and the FOR loop has stopped looping for infinitly, the problem that has arose now is that it's producing the number of results that it should be but all the results are duplicates of the newest results relating to the query. For example: The table contains two results relating to the user Abed, the reviews are both by Jeff one reads "yes" the other reads "no" - the "no" review is the newest entry. As there are two results related to Abed two results are produced by the FOR loop, but both of them read "no" when one should read "yes" and the other should read "no". Can someone help me with this issue? PHP: <div = "reviews"> <?php $sqlrev = "SELECT * FROM rev WHERE rev_name = '$member' ORDER BY id DESC"; $revresult = mysql_query($sqlrev) or die(mysql_error()); $revrows = mysql_num_rows($revresult); $revname = mysql_fetch_array($revresult); ?> User reviews for <? echo $member;?> <br> <br> <table border="0"> <?php for($count = 1; $count <= $revrows; $count++) { ?> <tr> <?php echo "Review by: " . $revname['usr_name']; echo "<br>"; echo $revname['text'] ."<br>"; echo "<br>"; ?> </tr> <?php } ?> </table> </div> Quote Link to comment https://forums.phpfreaks.com/topic/261339-review-script/#findComment-1339220 Share on other sites More sharing options...
DavidAM Posted April 20, 2012 Share Posted April 20, 2012 mysql_fetch_array returns the next row from your query. Since you are only calling it once, you are only looking at the first row. Since mysql_fetch_array() will return false if no row is returned, you don't need to use a counter for the loop. Consider something like this: <div = "reviews"> <?php $sqlrev = "SELECT * FROM rev WHERE rev_name = '$member' ORDER BY id DESC"; $revresult = mysql_query($sqlrev) or die(mysql_error()); $revrows = mysql_num_rows($revresult); // Don't really need this any more, either # $revname = mysql_fetch_array($revresult); MOVED THIS TO REPLACE THE FOR LOOP ?> User reviews for <? echo $member;?> <br> <br> <table border="0"> <?php // PROCESS EACH ROW RETURNED BY THE QUERY while ($revname = mysql_fetch_array($revresult)) { ?> <tr> <?php echo "Review by: " . $revname['usr_name']; echo "<br>"; echo $revname['text'] ."<br>"; echo "<br>"; ?> </tr> <?php } ?> </table> </div> Quote Link to comment https://forums.phpfreaks.com/topic/261339-review-script/#findComment-1339226 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.