Here is my code:
echo '<table class="class1"><tbody>';
while ($row1=mysql_fetch_array($result1)){
echo '<tr><td>'.$row1['firstname'].'</td><td>'.$row1['lastname'].'</td></tr>';
}
echo '</tbody></table>';
When I use the HTML validator, it returns a good and a proper error “trimming empty <tbody>“. It happens in the case when there are 0 rows selected. So, the code looks like <table><tbody></tbody></table>, and that is not good.
I’m thinking of the best way to exclude this error (do not display this table at all when there are 0 rows or something like that), but I don’t know the best way how to do that.
How’s the idea to check if mysql_num_rows()>0 before choosing to display echo '<table class="class1"><tbody>'; ? Any better ideas or is this one good?
Use
mysql_num_rows($result)to check whether the query has any results. If true, create a table + rows. Otherwise, do nothing, or add an alternative block: