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!!
trouble with a while loop
Moderator: General Moderators
Re: trouble with a while loop
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 />";
}
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 />";
}