title: PHP/MySQL group results by column
body: <p>in order to keep as few SQL statements as possible, I want to do select set from MySQL:</p>
  
  <pre><code>SELECT * FROM products WHERE category IN (10,120,150,500) ORDER BY category,id;
  </code></pre>
  
  <p>Now, I have list of products in following manner:</p>
  
  <pre><code>CATEGORY
   - product 1
   - product 2
  CATEGORY 2
   - product 37
  ...
  </code></pre>
  
  <p>What's the best and most efficent way to process MySQL result?</p>
  
  <p>I thought something like (pseudo PHP)</p>
  
  <pre><code>foreach ($product = fetch__assoc($result)){
    $products[$category][] = $product;
  }
  </code></pre>
  
  <p>and then when outputting it, do foreach loop:</p>
  
  <pre><code>foreach($categories as $category){
    foreach($products[$category] as $product){
      $output;
    }
  }
  </code></pre>
  
  <p>Is this the best, or is something magical like <code>mysql_use_groupby</code> or something?</p>