trouble with a while loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
crowegreg
Forum Newbie
Posts: 13
Joined: Tue Jul 19, 2011 2:48 pm

trouble with a while loop

Post by crowegreg »

This loop does exactly what I want it to do when $row2 contains records:
while($row2 = mysql_fetch_array($result2)) {
echo "<tr>";
echo "<td>" . $row2['0'] . "</td><td>" . $row2['1'] . "</td>";
echo "</tr>";
echo "<br />";


}

My problem I'd like for it to display an echo statement when $row2 contains no records. This is what I've tried without success:
while(!$row2 = mysql_fetch_array($result2)) {
echo "<tr>";
echo "<td>" . "there are no previous visits" . "</td>";
echo "</tr>";
echo "<br />";
} else $row2 = mysql_fetch_array($result2)) {
echo "<tr>";
echo "<td>" . $row2['0'] . "</td><td>" . $row2['1'] . "</td>";
echo "</tr>";
echo "<br />";
}

What am I doing wrong? Thanks for your suggestions!!
crowegreg
Forum Newbie
Posts: 13
Joined: Tue Jul 19, 2011 2:48 pm

Re: trouble with a while loop

Post by crowegreg »

I figured it out. Here's my code that worked:

if(mysql_num_rows($results2) > 0) {
while($row2 = mysql_fetch_array($result2)) {
echo "<tr>";
echo "<td>" . $row2['0'] . "</td><td>" . $row2['1'] . "</td>";
echo "</tr>";
echo "<br />";
}
} else {
echo "<tr>";
echo "<td>" . "there are no previous visits" . "</td>";
echo "</tr>";
echo "<br />";
}
Post Reply