Jump to content

While Loop / Variable Access Question


chordsoflife

Recommended Posts

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

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]));

                }

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.