title: How to generate JSON data with PHP?
body: <pre><code>CREATE TABLE Posts
  {
  id INT PRIMARY KEY AUTO_INCREMENT,
  title VARCHAR(200),
  url VARCHAR(200)
  }
  </code></pre>
  
  <p><strong>json.php code</strong></p>
  
  <pre><code>&lt;?php
  $sql=mysql_query("select * from Posts limit 20");
  echo '{"posts": [';
  while($row=mysql_fetch_array($sql))
  {
  $title=$row['title'];
  $url=$row['url'];
  echo '
  
  {
  
  "title":"'.$title.'",
  
  "url":"'.$url.'"
  
  },'; 
  }
  echo ']}';
  
  ?&gt;
  </code></pre>
  
  <p>I have to generate <code>results.json</code> file.</p>