title: PHP echos Out of Order
body: <p>I wrote a script in PHP that takes values from a MySQL Database and outputs the values as JSON. However, I'm having a problem. Here's the code:</p>
  
  <pre><code>    &lt;?php 
      mysql_connect("localhost", "root", "");
      mysql_select_db("stitchdex");
      $query = mysql_query("SELECT image, price, description FROM {$_GET['table']}");
      $jsonString = "{\"images\":[";
      $numrows = mysql_num_rows($query);
      $i=0;
      while ( $row = mysql_fetch_array($query) ) {
          $jsonString .= "{\"image\":\"{$row['image']}\", \"price\":\"{$row['price']}\", \"description\":\"{$row['description']}\"}";
          if ( ($numrows-1) == $i ) echo "";
          else echo ",";
          $i++;
      }
      $jsonString .= "]}";
      echo $jsonString;
  ?&gt;
  </code></pre>
  
  <p>But it outputs:</p>
  
  <pre><code>,,,,,,,,,,,,,,,,,{"images":[{"image":"priceguide1.jpg", "price":"", "description":"Diagram of Options"}{"image":"2ndamendfront.jpg", "price":"$109.00", "description":"Total of 3 laser etchings"}{"image":"DSC_0120.jpg", "price":"$99.00", "description":"Designer Kydex 1 Laser Etching"}{"image":"DSC_0121.jpg", "price":"", "description":"Lazer Etching on Back"}{"image":"gilbert.jpg", "price":"$109.00", "description":"Designer Kydex 2 Lazer Etchings"}{"image":"glock23-carbonfiber-red1.jpg", "price":"$79.00", "description":""}{"image":"irish1.jpg", "price":"$129.00", "description":"Designer Kydex Two Tone Leather &amp; 3 Laser Etchings Two on front One on Back"}{"image":"irish2.jpg", "price":"$129.00", "description":"Designer Kydex Two Tone Leather &amp; 3 Laser Etchings Two on front One on Back"}{"image":"klover-glock-2.jpg", "price":"$129.00", "description":"Designer Kydex Two Tone Leather &amp; 3 Laser Etchings Two on front One on Back"}{"image":"priceguide1.jpg", "price":"", "description":"Diagram of Options"}{"image":"2ndamendfront.jpg", "price":"$109.00", "description":"Total of 3 laser etchings"}{"image":"DSC_0120.jpg", "price":"$99.00", "description":"Designer Kydex 1 Laser Etching"}{"image":"DSC_0121.jpg", "price":"", "description":"Lazer Etching on Back"}{"image":"gilbert.jpg", "price":"$109.00", "description":"Designer Kydex 2 Lazer Etchings"}{"image":"glock23-carbonfiber-red1.jpg", "price":"$79.00", "description":""}{"image":"irish1.jpg", "price":"$129.00", "description":"Designer Kydex Two Tone Leather &amp; 3 Laser Etchings Two on front One on Back"}{"image":"irish2.jpg", "price":"$129.00", "description":"Designer Kydex Two Tone Leather &amp; 3 Laser Etchings Two on front One on Back"}{"image":"klover-glock-2.jpg", "price":"$129.00", "description":"Designer Kydex Two Tone Leather &amp; 3 Laser Etchings Two on front One on Back"}]}
  </code></pre>
  
  <p>This would be valid JSON if the commas separated the objects in the "images" array, but for some reason the script outputs all of the commas first. Why is this?</p>