title: PHP: can't encode json with multiple rows
body: <p>I've spent a couple of hours looking through several the similar answers before posting my problem.</p>
  
  <p>I'm retrieving data from a table in my database, and I want to encode it into a JSON. However, the output of json_encode() is only valid when the table has one single row. If there is more than one row, the test at <a href="http://jsonlint.com/">http://jsonlint.com/</a> returns an error.</p>
  
  <p>This is my query:</p>
  
  <pre><code>$result = mysql_query($query);
  
      $rows = array();
  
      //retrieve and print every record
      while($r = mysql_fetch_assoc($result)){
          $rows['data'] = $r;
  
          //echo result as json
          echo json_encode($rows);
      }
  </code></pre>
  
  <p>That gets me the following JSON:</p>
  
  <pre><code>{
  "data": 
      {
          "entry_id":"2",
          "entry_type":"Information Relevant to the Subject",
          "entry":"This is my second entry."
      }
  }
  
  
  {
  "data":{
          "entry_id":"1",
          "entry_type":"My Opinion About What Happened",
          "entry":"This is my first entry."
      }
   }
  </code></pre>
  
  <p>When I run the test at <a href="http://jsonlint.com/">http://jsonlint.com/</a>, it returns this error:</p>
  
  <pre><code>    Parse error on line 29:
      ..."No comment"    }}{    "data": {    
      ---------------------^
      Expecting 'EOF', '}', ',', ']'
  </code></pre>
  
  <p>However, if I only use this first half of the JSON...</p>
  
  <pre><code>{
  "data": 
      {
          "entry_id":"2",
          "entry_type":"Information Relevant to the Subject",
          "entry":"This is my second entry."
      }
  }
  </code></pre>
  
  <p>... or if I only test the second half...</p>
  
  <pre><code>{
      "data":{
          "entry_id":"1",
          "entry_type":"My Opinion About What Happened",
          "entry":"This is my first entry."
      }
   }
  </code></pre>
  
  <p>... the same test will return "Valid JSON".</p>
  
  <p>What I want is to be able to output in one single [valid] JSON every row in the table.</p>
  
  <p>Any suggestion will be very much appreciated.</p>