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

                }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.