title: PHP json_encode() in while loop
body: <p>I am trying to use <code>json_encode()</code> in a while loop while getting database results. Here is my code:</p>
  
  <pre><code>&lt;?
  
  $database = sqlite_open("thenew.db", 0999, $error);
  if(!$database) die($error);
  
  $query = "SELECT * FROM users";
  $results = sqlite_query($database, $query);
  if(!$results) die("Canot execute query");
  
  while($row = sqlite_fetch_array($results)) {
    $data = $row['uid'] . " " . $row['username'] . " " . $row['xPos'] . " " . $row['yPos'];
  }
  echo json_encode(array("response"=&gt;$data));
  
  sqlite_close($database);
  
  ?&gt;
  </code></pre>
  
  <p>The output of this is </p>
  
  <blockquote>
    <p>{"response":"lastUserID lastUser lastXPos lastYPos"}</p>
  </blockquote>
  
  <p>I want it to be...</p>
  
  <blockquote>
    <p>{"response":["1 Alex 10 12", "2 Fred 27 59", "3 Tom 47 19"]}</p>
  </blockquote>
  
  <p>etc.</p>
  
  <p>So I want the <code>json_encode()</code> function to put ALL users into the array rather than the last one. How would I do this? Thanks</p>