melting_dog Posted October 15, 2011 Share Posted October 15, 2011 Hi guys Probably a simple but trying to figure out why I cant get this while loop to work (at least I think its the loop) Query works as it returns 1 row, but not all of them. Any help would be great! $sql = "SELECT * FROM requests ORDER BY leave_after DESC"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_assoc($result)) { echo '<div id="resultholder">'; echo "<p>Request ID:" . $row['id'] . ", User ID: " . $row['user_id'] . ", Departing From: " . $row['departing'] . ", Going To: " . $row['destination'] . " , Leaving After: " . $row['leave_after'] . " , but Arriving Before: " . $row['arrive_before'] ."</p></div>"; echo '</div>'; } } } Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/249164-trying-to-figure-out-why-this-while-loop-isnt-working/ Share on other sites More sharing options...
codefossa Posted October 15, 2011 Share Posted October 15, 2011 Just do the query and then the while loop. The if statements ain't important because the while loop don't run unless there's something to loop through (it's not empty). Quote Link to comment https://forums.phpfreaks.com/topic/249164-trying-to-figure-out-why-this-while-loop-isnt-working/#findComment-1279548 Share on other sites More sharing options...
Psycho Posted October 15, 2011 Share Posted October 15, 2011 I don't see any reason why all the records would not be displayed = although I see you have two closing DIVs and only one opening div. But, i agree that the embedded conditions are a little odd. Are you positive there are more than 1 record in the table? Try the following: $query = "SELECT * FROM requests ORDER BY leave_after DESC"; $result = mysql_query($query); if (!$result) { echo "Query failed: " . mysql_error(); } else { //Debug line echo "Rows returned: " mysql_num_rows($result) . "<br>\n"; while ($row = mysql_fetch_assoc($result)) { echo "<div id='resultholder'><p>"; echo "Request ID: {$row['id']}, "; echo "User ID: {$row['user_id']}, "; echo "Departing From: {$row['departing']}, "; echo "Going To: {$row['destination']}, "; echo "Leaving After: {$row['leave_after']}, "; echo "but Arriving Before: {$row['arrive_before']}" echo "</p></div>\n"; } } Quote Link to comment https://forums.phpfreaks.com/topic/249164-trying-to-figure-out-why-this-while-loop-isnt-working/#findComment-1279558 Share on other sites More sharing options...
PFMaBiSmAd Posted October 16, 2011 Share Posted October 16, 2011 I'm going to guess that in your actual code, you are running another query inside of that loop and you are overwriting the $result variable so that when the loop condition is evaluated to start the second pass through the loop, $result no longer contains the result resource from the SELECT query. Quote Link to comment https://forums.phpfreaks.com/topic/249164-trying-to-figure-out-why-this-while-loop-isnt-working/#findComment-1279733 Share on other sites More sharing options...
melting_dog Posted October 17, 2011 Author Share Posted October 17, 2011 Hi guys, Thanks for your help and sorry for the delayed reply. Yes, as it was the loop was working fine. The extra </div> was the spanner in the works hiding my results completely. Sorry - should have properly checked my code before posting. Thanks guys! Quote Link to comment https://forums.phpfreaks.com/topic/249164-trying-to-figure-out-why-this-while-loop-isnt-working/#findComment-1279851 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.