Page 1 of 1

trouble with a while loop

Posted: Sat Jul 23, 2011 11:50 am
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!!

Re: trouble with a while loop

Posted: Sat Jul 23, 2011 12:11 pm
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 />";
}