Q:

Foreach loop are displaying same rows multiple times

I have a problem with my first attempt at making a foreach loop. 
My problem is, that I'm only trying to call out two rows to be displayed, which kinda works, although they are being displayed as many times as there are different rows in my table.
My code looks like this:
$sql = "SELECT * FROM webpages";
$result = mysql_query($sql);
$assoc_query = mysql_fetch_assoc($result);

<?php foreach ($assoc_query as $value) { ?>
<tr>
   <td>
     <div id='pageimg'><img src= <?php echo $assoc_query['pic'];?>  ></div>
   </td>
   <td>
     <div id="pagename"><?php echo $assoc_query['name']; ?> </div>
   </td>
</tr>
<?php } ?>

It's being displayed like so on the page: 
/picture/ DAK
/picture/ DAK
/picture/ DAK
/picture/ DAK

Hope you can help me:)

A:

Instead of foreach you will want a while. What you are doing here is looping the elements of array $assoc_query.
A while loop will get the next result set to be used.
while($assoc_query = mysql_fetch_assoc($result)){
    //Do your thing
}

Also please consider updating from mysql_ to mysqli_ or PDO. What you are using is depreciated and there is added security with the newer ones.

