echo '<table><tr>'; $i = 1; while($fila=mysql_fetch_assoc($result)) { echo'<td>'.$fila['foto'].'</td>'; if ($i == 5) { echo '</tr><tr>'; $i = 1; } else $i++;} echo '</tr></table>';