chordsoflife Posted March 26, 2009 Share Posted March 26, 2009 Here's the relevant code: $shows = mysql_query("SELECT * FROM tblConcert WHERE fkBandID = '$pk' AND fldConcertDate >= CURDATE() ORDER BY fldConcertDate", $db); $numRows = mysql_num_rows($shows); while($show = mysql_fetch_array($shows, MYSQL_ASSOC)){ $date = date("F j, Y",strtotime($show[fldConcertDate])); if($numRows == 0){ echo "<p>No upcoming shows for $show[fldBand]. <a href='#'>Know of one?</a></p>"; } else { This doesn't work. I assume it's a simple mistake (I'm self taught so I lack a lot of basics), but $numRows is acting like it doesn't exist. If I move that if statement outside of the while loop, it does work, and I guess I can do that, but I'm asking for the learning experience rather than a fix so... why can't I use the variable inside the loop? Link to comment https://forums.phpfreaks.com/topic/151291-while-loop-variable-access-question/ Share on other sites More sharing options...
taquitosensei Posted March 26, 2009 Share Posted March 26, 2009 I would put the number of rows before the loop $shows = mysql_query("SELECT * FROM tblConcert WHERE fkBandID = '$pk' AND fldConcertDate >= CURDATE() ORDER BY fldConcertDate", $db); $numRows = mysql_num_rows($shows); if($numRows == 0){ echo "<p>No upcoming shows for $show[fldBand]. <a href='#'>Know of one?</a></p>"; } else { while($show = mysql_fetch_array($shows, MYSQL_ASSOC)){ $date = date("F j, Y",strtotime($show[fldConcertDate])); } Link to comment https://forums.phpfreaks.com/topic/151291-while-loop-variable-access-question/#findComment-794727 Share on other sites More sharing options...
chordsoflife Posted March 26, 2009 Author Share Posted March 26, 2009 Agreed, that does work, but I'm more wondering why what I have does NOT work. I've used "$count" successfully after setting it outside of a loop, so the logic is where I'm fuzzy on this one. Link to comment https://forums.phpfreaks.com/topic/151291-while-loop-variable-access-question/#findComment-794729 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.