title: How to use PHP foreach with MySql
body: <p>I want to loop through each result from the <code>mysql</code> table and generate html <code>div</code> for each rows. The database table looks like following:</p>
  
  <pre><code>--------------------------------
    id   |  color  | width | img
  --------------------------------
     1   |  red    |  550  | url
  --------------------------------
     2   |  black  |  650  | url
  --------------------------------
  </code></pre>
  
  <p>I have an idea of how this might work but not sure about the syntax. Below is my attempt.</p>
  
  <pre><code>&lt;?php
  // connect to the database
  
  $rows = // get rows from the db table
  
  foreach ($rows) {
  &lt;div style="background-color:$color;width:$width;"&gt;
      &lt;img src="$img"&gt;
  &lt;/div&gt;
  }
  </code></pre>