<?php $query = "Select * from products"; $result = $db_mysql->query($sql); $i = 0; while ($row = mysql_fetch_row($result)) { if ($i % 3 == 0) echo "<tr>"; echo "<td>".$row["id"]."</td>"; $i++; } ?>