title: PHP MySqli GROUP BY
body: <p>I have a database where I house my clients, I need to display them on a page grouped and ordered by State and City.</p>
  
  <p>If I use the code below I get the result I want but the State duplicates for each city.
  If I change the ORDER BY to GROUP BY then it only displays 1 result, there are several cities in the State and I obviously want them all listed under the relevant State.</p>
  
  <p>Any help is welcome.</p>
  
  <pre><code>"SELECT state, city, company_name  FROM mytable WHERE status='T' AND city &lt;&gt;'Head Office' AND company_name &lt;&gt;'my company' ORDER BY state ASC"
  </code></pre>
  
  <p>Further code as requested below:</p>
  
  <pre><code>$coverage="";
  $sql = "SELECT state, city, company_name  FROM mutable WHERE status='T' AND city &lt;&gt;'Head Office' AND company_name &lt;&gt;'my company' GROUP BY state, city ORDER BY state ASC, city ASC";
  $result = mysqli_query($conn, $sql);
  if (mysqli_num_rows($result) &gt; 0) {
  $currentCity = NULL;
  while($row = mysqli_fetch_assoc($result)) { 
      $id = $row["id"]; 
      $state =$row["state"];
      $office = $row["city"]; 
      $agent = $row["company_name"]; 
      if($row["city"] !== $currentCity) {
          $currentCity = $row["city"];
          $coverage .= '
          &lt;div id="stateCoverage" class="fluid "&gt;
          &lt;p&gt;&lt;strong class="white"&gt;' . $state . '&lt;/strong&gt;&lt;/p&gt;
          &lt;/div&gt;
              &lt;div id="officeCoverage" class="fluid "&gt;
                  &lt;h3&gt;' . $office . '&lt;/h3&gt;
              &lt;/div&gt; 
          ';
      }
      $coverage .= '
          &lt;div id="imageCoverage" class="fluid "&gt;
                  &lt;img src="http://foo/bar/logos/' . $agent . '.jpg" alt="' . $agent . '"&gt;
              &lt;/div&gt;
      ';
  
  }
  }else{
  $coverage="&lt;p&gt;There seems to have been a problem locating your content, please try again, if the problem persists we would be grateful if you reported it to us, Thank you.&lt;/p&gt;";
  }
  </code></pre>