title: Effective wayto sum values in MySQL table rows
body: <p>I have a MySQL table that looks something like this:</p>
  
  <pre><code>+--------------------------------------+
  |   id   |    product_id   |   qty     |
  +--------------------------------------+
  |    1   |        0        |    1      |
  |    2   |        1        |    3      |
  |    3   |        0        |    2      |
  |    4   |        2        |    18     |
  +--------------------------------------+
  </code></pre>
  
  <p>I want to get the total number of each product in the table. So for the above table, for instance, here is the result I would like:</p>
  
  <p>0 -> 3<br>
  1 -> 3<br>
  2 -> 18  </p>
  
  <p>I figured the easiest way to do this would be to loop through the MySQL results and add the quantity of each product to an array, at the position in the array that corresponds to the product_id. I.E:</p>
  
  <pre><code>$qtyArray = array();
  
  while($row = mysql_fetch_array($result, MYSQL_ASSOC)) 
  {
      $qtyArray[$row[product_id]] += $row[qty];
  }
  </code></pre>
  
  <p>I have two questions:</p>
  
  <ol>
  <li>Would the above work OK?</li>
  <li>Is there a better way of doing this?</li>
  </ol>
  
  <p>Thank you!</p>