title: While loop and json
body: <p>I am working with Twitter Typeahead <a href="http://twitter.github.io/typeahead.js/examples/#prefetch" rel="nofollow">prefetch</a> to search a (json) list of books. </p>
  
  <p>I need to create an array of objects which contains both the <code>title</code> of the book and the <code>publication_date</code> - I'm just not sure how! I've only managed to get the title so far.</p>
  
  <p>The code I have at present works, however it only returns the book title, I'm not sure what I have to change in order to also retrieve the publication_date. </p>
  
  <p>Maybe I need a foreach instead of a while loop? Quite new to this and learning as I go.</p>
  
  <p>My code at present;</p>
  
  <pre><code>&lt;?php
  include(URL. '/lib/conn.php'); // include the db connection
  
  $sql = ("SELECT * FROM publication");
  
  if(!$result = $db-&gt;query($sql)){
      die('There was an error running the query [' . $db-&gt;error . ']');
  }
  
  $data = array();
  
  while($row = $result-&gt;fetch_assoc()){
      $data[] = utf8_encode($row['title']);
  }
  
  file_put_contents(URL . '/title.json', json_encode($data)); 
  
  ?&gt;
  </code></pre>
  
  <p>The code above successfully creates a <code>title.json</code> and populates it with a list of book titles.</p>
  
  <p>Any advice or direction is appreciated.</p>