yo lo hago de la siguiente manera :
Código PHP:
getimgs = mysql_query("SELECT * FROM imagenes WHERE categoria='".$id."' ORDER by id ASC");
echo "<br>
<table align=center>
";
while ($row2 = mysql_fetch_array($getimgs))
{
$cont++;
if ($cont==3) { echo "<tr></tr>"; $cont=0;}
echo '
<td><a href="javascript:popUp2(\'verimagen.php?id='.$row2["id"].'&cat='.$row2["categoria"].'\')"><img src="'.$row2["link"].'" width="173" height="150" border="0"></a></td>
';
}
echo "</table>";
espero te sirva saludos
:)